servlet程序中抛出java.lang.nullpointer异常
感谢大家的精彩回答。我尝试了@Jan、@Ratna 所说的一切,但我无法克服这个异常。我的 servlet 程序中的第 52 行是
Statement stmt=connect.createStatement();
我在这里展示更多的代码:
public class s3 extends HttpServlet
{
String message,seat_no,name,ans1,ans2,ans3,ans4,ans5;
int Total=0;
Connection connect;
Statement stmt=null;
ResultSet rs=null;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try
{
String url="jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
connect=DriverManager.getConnection(url,"root","root");
message="Connection Sucessfull";
}
catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
catch(Exception excp){
excp.printStackTrace();
}
seat_no=request.getParameter("seat_no");
name=request.getParameter("name");
ans1=request.getParameter("group1");
ans2=request.getParameter("group2");
ans3=request.getParameter("group3");
ans4=request.getParameter("group4");
ans5=request.getParameter("group5");
if(Boolean.getBoolean(ans1) == true)
Total+=2;
if(Boolean.getBoolean(ans2) == false)
Total+=2;
if(Boolean.getBoolean(ans3) == true)
Total+=2;
if(Boolean.getBoolean(ans4) == false)
Total+=2;
if(Boolean.getBoolean(ans5) == false)
Total+=2;
try
{
if(Total>0)// Just a condition.. !
{
Statement stmt=connect.createStatement();
String query="INSERT INTO st1("+"seat_no,name,marks"+")VALUES('"+seat_no+"','"+name+"','"+Total+"')";
stmt.executeUpdate(query);
stmt.close();
}
}
catch(SQLException ex){
}
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body bgcolor=pink>");
out.println("<h1>"+message+"</h1>\n");
out.println("<h3>database updated");
out.println("<br>");
out.println("<b>"+"datbase is follows");
out.println("<table border=5>");
try
{
Statement stmt=connect.createStatement();
String query="SELECT * FROM st1";
rs=stmt.executeQuery(query);
out.println("<th>"+"seat_no"+"</th>");
out.println("<th>"+"name"+"</th>");
out.println("<th>"+"marks"+"</th>");
您的宝贵建议将不胜感激!
Thanks for those wonderful answers everyone.I tried everything said by @Jan, @Ratna yet I am not able to overcome the exception. Line No.52 in my servlet program is
Statement stmt=connect.createStatement();
I present more of the code here:
public class s3 extends HttpServlet
{
String message,seat_no,name,ans1,ans2,ans3,ans4,ans5;
int Total=0;
Connection connect;
Statement stmt=null;
ResultSet rs=null;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try
{
String url="jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
connect=DriverManager.getConnection(url,"root","root");
message="Connection Sucessfull";
}
catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
catch(Exception excp){
excp.printStackTrace();
}
seat_no=request.getParameter("seat_no");
name=request.getParameter("name");
ans1=request.getParameter("group1");
ans2=request.getParameter("group2");
ans3=request.getParameter("group3");
ans4=request.getParameter("group4");
ans5=request.getParameter("group5");
if(Boolean.getBoolean(ans1) == true)
Total+=2;
if(Boolean.getBoolean(ans2) == false)
Total+=2;
if(Boolean.getBoolean(ans3) == true)
Total+=2;
if(Boolean.getBoolean(ans4) == false)
Total+=2;
if(Boolean.getBoolean(ans5) == false)
Total+=2;
try
{
if(Total>0)// Just a condition.. !
{
Statement stmt=connect.createStatement();
String query="INSERT INTO st1("+"seat_no,name,marks"+")VALUES('"+seat_no+"','"+name+"','"+Total+"')";
stmt.executeUpdate(query);
stmt.close();
}
}
catch(SQLException ex){
}
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body bgcolor=pink>");
out.println("<h1>"+message+"</h1>\n");
out.println("<h3>database updated");
out.println("<br>");
out.println("<b>"+"datbase is follows");
out.println("<table border=5>");
try
{
Statement stmt=connect.createStatement();
String query="SELECT * FROM st1";
rs=stmt.executeQuery(query);
out.println("<th>"+"seat_no"+"</th>");
out.println("<th>"+"name"+"</th>");
out.println("<th>"+"marks"+"</th>");
Your valuable suggestions will be appreciated !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MySQL 手册,您 需要调用
在尝试打开连接之前 。您将因
forName()
调用而停止。According to the MySQL manual, you need to call
before trying to open a connection. You're stopping with the
forName()
call.