首页 新闻 搜索 专区 学院

JSP中使用prepareStatement出现问题

0
悬赏园豆:10 [已解决问题] 解决于 2013-05-15 08:53
<% 
    //jspexdb.setStr("hello world!");
   // out.println(jspexdb.getStr());
    //jspexdb  = new JspexBean();
        //JspexBean jBean = new JspexBean();
     //jspexbean;
         String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL = "jdbc:sqlserver://192.168.1.117:1443; DatabaseName=CRM";
        String userName = "sa";
        String userPwd = "mzyuan";
        Connection dbConn = null;
        //Connection conn = null;
        try
        {
            Class.forName(driverName).newInstance();
            out.println("驱动程序已加载");
        }
        catch(Exception e)
        {
            out.println("加载驱动程序错误...");
            e.printStackTrace();
        }
        try
        {
            dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
            out.println("connection Successful!");
        }
        catch (SQLException e)
        {
            System.out.println("数据库连接错误...");
            e.printStackTrace();
        }
     /*
     if (null == jspexbean)
     {
         out.println("error");
     }
     else
     {
     out.println("success");
     }
     String sqlStr = "SELECT [userid],[username],[password],[email],[flag] FROM [CRM].[dbo].[user]";
     ResultSet rs = jspexbean.reslutSet(null,sqlStr);
     */
     String sqlStr = "SELECT userid,username,password,email,flag FROM dbo.user";
     PreparedStatement pst = dbConn.prepareStatement(sqlStr);
     pst.executeQuery();
     ResultSetMetaData rsmd = pst.getMetaData();
     out.println(rsmd.getColumnCount());
     out.println(rsmd.getTableName(1));
     %>

主要是在

PreparedStatement pst = dbConn.prepareStatement(sqlStr);这一句出现问题
具体问题描述如下:

message An exception occurred processing JSP page /index-ec.jsp at line 72

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /index-ec.jsp at line 72

69:      ResultSet rs = jspexbean.reslutSet(null,sqlStr);
70:      */
71:      String sqlStr = "SELECT userid,username,password,email,flag FROM dbo.user";
72:      PreparedStatement pst = dbConn.prepareStatement(sqlStr);
73:      pst.executeQuery();
74:      ResultSetMetaData rsmd = pst.getMetaData();
75:      out.println(rsmd.getColumnCount());


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

 

root cause

java.lang.NullPointerException
	org.apache.jsp.index_002dec_jsp._jspService(index_002dec_jsp.java:138)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
 
芝麻学问的主页 芝麻学问 | 初学一级 | 园豆:167
提问于:2013-05-13 11:56
< >
分享
最佳答案
0
dbConn没有正常初始化好吧
debug看看排查
收获园豆:5
2012 | 高人七级 |园豆:20812 | 2013-05-13 12:33

找到问题了。sql语句写错了

芝麻学问 | 园豆:167 (初学一级) | 2013-05-15 08:52
其他回答(1)
0

首先你要确定数据库连接是否正常,如果正常可以试试statement

收获园豆:5
cel | 园豆:380 (菜鸟二级) | 2013-05-14 16:06

找到问题了。sql语句写错了

支持(0) 反对(0) 芝麻学问 | 园豆:167 (初学一级) | 2013-05-15 08:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册