我想用Servlet显示一个简单的成绩单表格,用Eclipse+Tomcat+Mysql,运行时出现问题。
我的Servlet代码如下:
package servlet.test;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Chjidan extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String driver = "com.mysql.jdbc.Driver";
String dbURL="jdbc:mysql//localhost:3306/students";
String dbUser = "root";
String dbPwd = "007";
String sql="select xh,xm,zxf,zscore from chjidan";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf8");
PrintWriter out=response.getWriter();
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
conn=DriverManager.getConnection(dbURL,dbUser,dbPwd);
} catch(SQLException e){
e.printStackTrace();
}
try {
stmt = conn.createStatement();
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
out.println("<html><head><title>学生成绩单" +
"</title></head><body><table border='1'><tr><th>学号" +
"</th><th>姓名"+"</th><th>总学分"+"</th><th>总成绩"+"</th></tr>");
try{
while(rs.next()){
out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td></tr>");
}
}catch(SQLException e){
e.printStackTrace();
}
out.println("</table></body></html>");
out.close();
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
运行后弹出错误页:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
servlet.test.Chjidan.doGet(Chjidan.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.我能肯定的一点是数据库驱动肯定没问题,因为其它的一些jsp文件可以连接到数据库,哪位大侠能帮忙解决,小弟将感激不尽。
我的Servlet代码如下:
package servlet.test;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Chjidan extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String driver = "com.mysql.jdbc.Driver";
String dbURL="jdbc:mysql//localhost:3306/students";
String dbUser = "root";
String dbPwd = "007";
String sql="select xh,xm,zxf,zscore from chjidan";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf8");
PrintWriter out=response.getWriter();
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
conn=DriverManager.getConnection(dbURL,dbUser,dbPwd);
} catch(SQLException e){
e.printStackTrace();
}
try {
stmt = conn.createStatement();
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
out.println("<html><head><title>学生成绩单" +
"</title></head><body><table border='1'><tr><th>学号" +
"</th><th>姓名"+"</th><th>总学分"+"</th><th>总成绩"+"</th></tr>");
try{
while(rs.next()){
out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td></tr>");
}
}catch(SQLException e){
e.printStackTrace();
}
out.println("</table></body></html>");
out.close();
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
运行后弹出错误页:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
servlet.test.Chjidan.doGet(Chjidan.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.我能肯定的一点是数据库驱动肯定没问题,因为其它的一些jsp文件可以连接到数据库,哪位大侠能帮忙解决,小弟将感激不尽。
Chjidan.java的38行有空指针异常,加一个判断语句就可以了
java.lang.NullPointerException 空指针异常,好好检查下,是否字段写错了
while(rs.next()){
out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td></tr>");
}
你的rs 的得到的小标是 从 ”1“ 开始的????
是这样么????。空指针。。楼主 百度一下吧。。如果你是做java 的 这都不知道的话出了门不要给别人说 你是从事软件行业的。百度一下吧。解释很详细。。
while(rs.next()){
out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td></tr>");
}
换成rs.getString("ColumName"),里面加列名,因为Statement 不能这样引用:rs.getString(1)
,好像要PrapareStatement
2.利用排除法,写个单独的class类测试是否能够运行成功,成功的基础上在去检查servlet提点建议:
1.servlet 是用来展现数据不是用来处理数据的,应该把上面的业务处理分开来写,便于维护与找错
2.try catch块太多,可读性太差了,尽量封装到一个try catch块中问的问题不明确,出错的原因很多,很难让人明确的给你解答,自己试着去调试吧!
stmt = conn.createStatement();
也就是说conn=DriverManager.getConnection(dbURL,dbUser,dbPwd);
没有创建conn对象。---
不知道定位的对不。。楼主给38行代码
急什么呀,汗…慢慢来,是个bug总会解决的
要加个创建conn对象的语句
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Chjidan extends HttpServlet {
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String driver = "com.mysql.jdbc.Driver";
String dbURL="jdbc:mysql//localhost:3306/students";
String dbUser = "root";
String dbPwd = "007";
String sql="select xh,xm,zxf,zscore from chjidan";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf8");
PrintWriter out=response.getWriter();
try {
Class.forName(driver);
conn=DriverManager.getConnection(dbURL,dbUser,dbPwd);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
out.println("<html><head><title>学生成绩单" +
"</title></head><body><table border='1'><tr><th>学号" +
"</th><th>姓名"+"</th><th>总学分"+"</th><th>总成绩"+"</th></tr>");
while(rs.next()){
out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td></tr>");
}
out.println("</table></body></html>");
out.close();
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}