我在myeclipse中用odbc连接数据库,但是每次进行数据库查询时一定要重新运行tomcat,为什么?
就是说,我查询一次后要重新运行tomcat才能再次查询,代码:packageco;importjava.io.IOException;importjava.sql.*;i...
就是说,我查询一次后要重新运行tomcat才能再次查询,代码:
package co;
import java.io.IOException;
import java.sql.*;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet {
private Connection con=null;
private ResultSet rst=null;
private PreparedStatement pstm=null;
private Statement stm=null;
public login() {
super();
}
public void destroy() {
super.destroy();
try{ if(rst!=null) rst.close();
if(pstm!=null)pstm.close();
if(con!=null)con.close();
if(stm!=null)stm.close();
}catch(Exception e){e.getStackTrace();}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"gbk");
String pwd=new String(request.getParameter("pwd").getBytes("iso-8859-1"),"gbk");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
String sql="select * from users where u_name='"+name+"'and password='"+pwd+"'";
try{
stm=con.createStatement();
rst=stm.executeQuery(sql);
if(rst.next()){
out.print("登录成功");
}else {out.print("登录失败");
}
}catch(Exception e){e.getStackTrace();}
//out.print("登录失败");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动程序
}
catch(ClassNotFoundException e)
{ }
try{
con=DriverManager.getConnection("jdbc:odbc:gd","GD","r22");
}
catch(SQLException e)
{ }
}
}
问题出在哪?请高手帮帮忙!! 展开
package co;
import java.io.IOException;
import java.sql.*;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet {
private Connection con=null;
private ResultSet rst=null;
private PreparedStatement pstm=null;
private Statement stm=null;
public login() {
super();
}
public void destroy() {
super.destroy();
try{ if(rst!=null) rst.close();
if(pstm!=null)pstm.close();
if(con!=null)con.close();
if(stm!=null)stm.close();
}catch(Exception e){e.getStackTrace();}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"gbk");
String pwd=new String(request.getParameter("pwd").getBytes("iso-8859-1"),"gbk");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
String sql="select * from users where u_name='"+name+"'and password='"+pwd+"'";
try{
stm=con.createStatement();
rst=stm.executeQuery(sql);
if(rst.next()){
out.print("登录成功");
}else {out.print("登录失败");
}
}catch(Exception e){e.getStackTrace();}
//out.print("登录失败");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动程序
}
catch(ClassNotFoundException e)
{ }
try{
con=DriverManager.getConnection("jdbc:odbc:gd","GD","r22");
}
catch(SQLException e)
{ }
}
}
问题出在哪?请高手帮帮忙!! 展开
2个回答
展开全部
项目里的servlet,init只在启动的时候调用一次!后面就不调用了,也就是说,你运行一次后,就没有连接conn了,肯定报的是找不到conn吧?或者createStatement空指针?把这个conn的创建写到doGet方法里去吧
更多追问追答
追问
第一次就输出登录成功
可是再次查询时就什么都没有输出,也没有报错
追答
你的代码我看有两个问题.一个就是servlet启动只是一次,二个是你运行完了数据库连接后好多的关闭没有做,具体是什么情况就不好说了,因为你那什么错都没报
你可以先按我说的做做看吧.然后再记得关闭rs这些数据库使用对象
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询