首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

好心人帮帮忙解决一下数据库连接的有关问题!

2011-12-14 
好心人帮帮忙解决一下数据库连接的问题!急!急!请高手们帮我看看这个问题,搞了好几次都出现错误(已经加载了

好心人帮帮忙解决一下数据库连接的问题!急!急!
请高手们帮我看看这个问题,搞了好几次都出现错误(已经加载了sqlserver的3个驱动文件),源程序和出现的错误提示如下:

package   shop;
import   java.sql.*;

public   class   DBConnectBean   {
    public   static   Connection   getSqlserverConn()
    {
      Connection   conn=null;
      String   url= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
      String   sqldriver= "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=jspdev ";
    try   {
        Class.forName(url);
        conn=DriverManager.getConnection(sqldriver, "sa ", " ");
        conn.setCatalog( "jspdev ");
    }
    catch   (Exception   ex)   {
        ex.printStackTrace();

    }
        return   conn;

    }
}
---------------------------------------------------
<%@   page   contentType= "text/html;   charset=GBK "   import= "java.sql.* "%>
<html>
<head>
<title>
logincheck.jsp
</title>
</head>
<body   bgcolor= "#ffffff ">
<jsp:useBean   id= "dbconn "   class= "shop.DBConnectBean "   scope= "page "/>
<%
//获得请求参数
  String   name=request.getParameter( "username ");
  String   password=request.getParameter( "userpassword ");
  //连接数据库
    dbconn.getSqlserverConn();
    String   usersql= "select   *   from   users   where   name= ' "+name+ " 'and   password= ' "+password+ " ' ";
    Connection   con=null;
      Statement   stmt=con.createStatement();
      ResultSet   result=stmt.executeQuery(usersql);
      if(result.next())
    {
        %>
  欢迎你, <%=name%>
    <%}%>
</body>
</html>
------------------------------------------------
java.lang.NullPointerException

java.lang.NullPointerException

at   org.apache.jsp.logincheck$jsp._jspService(logincheck$jsp.java:96)

at   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)

at   javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at   org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)

at   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)

at   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)

at   javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

at   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

at   org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at   org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)



at   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)

at   org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at   org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)

at   org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at   org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at   org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)

at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)

at   org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at   org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)

at   org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)

at   org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at   org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)

at   org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at   org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at   org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)

at   org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)

at   java.lang.Thread.run(Thread.java:536)



[解决办法]
Connection con=null;
Statement stmt=con.createStatement();
con是null,你使用他就会报空指针异常.
应该把Connection con=null;去掉
在dbconn.getSqlserverConn();的前面加上:
Connection con = dbconn.getSqlserverConn();

热点排行