为什么我的servlet连接mysql数据库时,总发生以下错误(我用的是tomcat服务器):

[复制链接]
查看11 | 回复3 | 2011-5-8 14:43:57 | 显示全部楼层 |阅读模式
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
        HelloServletJdbc.doPost(HelloServletJdbc.java:50)
        HelloServletJdbc.doGet(HelloServletJdbc.java:11)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.14
Connection conn = null;
                Statement st = null;
                ResultSet rs = null;
                resp.setContentType("text/html");
                resp.setCharacterEncoding("gb2312");
                PrintWriter out = resp.getWriter();
                try {
                        Class.forName("com.mysql.jdbc.Driver");
                        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs?user=root&password=6850262");
                        st = conn.createStatement();
                        rs = st.executeQuery("select * from aa");                       
                        while (rs.next()) {
                                out.println(rs.getString("myname"));
                        }
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        if (rs != null) {
                                try {
                                        rs = null;
                                        rs.close();
                                        st.close();
                                        conn.close();
                                } catch (SQLException e) {
                                        e.printStackTrace();
                                }
                        }
                }

回复

使用道具 举报

千问 | 2011-5-8 14:43:57 | 显示全部楼层
推测原因:1、数据库没有打开,解决方法:打开服务器;2、驱动包没有加进去,解决办法:下载mysql驱动包->项目->右击->properties->java build path->libraries-> add external jars(这是在myeclipse下的流程,如果你用的是其他的IDE你再百度一下);3、连接字符串写错,解决办法:仔细检查你的URL是否正确。
回复

使用道具 举报

千问 | 2011-5-8 14:43:57 | 显示全部楼层
空指针,看看50行是什么内容
回复

使用道具 举报

千问 | 2011-5-8 14:43:57 | 显示全部楼层
请求被阻止了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行