这是代码。
String Driver="com.mysql.jdbc.Driver";
String Url="jdbc:mysql://localhost:3306/studentdata";
String username="root";
String password="";
Class.forName(Driver);
Connection con=null;
Statement stment=null;
try{
con=DriverManager.getConnection(Url,username,password);
String Sqlquery="INSERT INTO `students`(`Name`, `FatherName`, `ClassNo`,
`Phone`, `Address`)"+
"VALUES(" + "'" +request.getParameter("studentName")+ "'" + "," + "'"
+ request.getParameter("FatherName")+ "'" + ","
+ "'" + request.getParameter("ClassNo")+ "'" + ","
+ "Address= "+ "'" + request.getParameter("studentAddress")+ "'" + ","
+ "Phone= "+ "'" + request.getParameter("studentPhone")+ "'" + ");";
stment=con.createStatement();
stment.executeUpdate(Sqlquery);
stment.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
String Selectquery="SELECT * FROM students";
ResultSet result=null;
try{
stment=con.createStatement();
result=stment.executeQuery(Selectquery);
}catch(SQLException e){
e.printStackTrace();
}
while(result.next()){
String Student_Id=result.getString(1);
String Student_Name=result.getString(2);
String Father_Name=result.getString(3);
String Class_No=result.getString(4);
String Address=result.getString(5);
String PhoneNo=result.getString(6);
}
当Compiler到达Select Query的createStatement时。它给出了NullPointerException的异常,并且这两个代码都在同一个函数_JspService中。我在scriplet中写了这个来检查数据库连接。
这是我得到的例外。
" org.apache.jasper.JasperException:处理JSP页面时发生异常 第97行的/TestDatabase.jsp
97:stment=con.createStatement();
" 根本原因
java.lang.NullPointerException
org.apache.jsp.TestDatabase_jsp._jspService(TestDatabase_jsp.java:166)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServlet
"