import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class stuDB extends HttpServlet
{
private String url;
private String username;
private String password;
public void init() throws ServletException
{
String driverClass=getServletContext().getInitParameter("driverClass");
url=getServletContext().getInitParameter("url");
username=getServletContext().getInitParameter("username");
password=getServletContext().getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new ServletException("加载数据库驱动失败!");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Connection conn=null;
Statement stmt=null;
resp.setContentType("text/html;charset=gb2312");
PrintWriter out=resp.getWriter();
try
{
conn=DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
ResultSet rs=null;
//stmt.executeUpdate("IF EXISTS(SELECT name FROM sysdatabases WHERE name='students') DROP DATABASE students");
stmt.executeUpdate("USE MASTER");
stmt.executeUpdate("IF EXISTS(SELECT name FROM sysdatabases WHERE name='students') DROP DATABASE students");
stmt.executeUpdate("create database students");
stmt.executeUpdate("use students");
stmt.executeUpdate("create table inf(sno VARCHAR(10) not null primary key,sname VARCHAR(10) not null,sclass int)");
stmt.addBatch("insert into inf values('s01','sss',1)");
stmt.executeBatch();
//conn=DriverManager.getConnection(url,username,password);
/*PreparedStatement insertStatement = conn.prepareStatement("INSERT INTO studentmessage values(?,?,?)");
insertStatement.setString(1, "S0");
insertStatement.setString(2, "依依");
insertStatement.setString(3, "女");
insertStatement.executeUpdate();*/
conn = DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
String sqlStr="select sno,sname,sclass from inf";
rs=stmt.executeQuery(sqlStr);
out.println("<html><head>");
out.println("<title>学生表</title>");
out.println("</head><body>");
out.println("<table border=1><caption>学生表</caption>");
out.println("<tr><th>学号</th><th>姓名</th><th>班级</th></tr>");
while(rs.next())
{
out.println("<tr>");
out.println("<td>"+rs.getString("sno")+"</td>");
out.println("<td>"+rs.getString("sname")+"</td>");
out.println("<td>"+rs.getInt("sclass")+"</td>");
out.println("</tr>");
}
out.println("</table></body></html>");
resp.setContentType("text/html;charset=gb2312");
out.println("数据库创建成功!");
out.close();
}
catch(SQLException se)
{
throw new ServletException(se);
}
finally{
if(stmt!=null)
{
try
{
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
}
运行时 出现 Allocate exception for servlet stuDB
java.lang.NullPointerException 错误~~
因为没有中文提示 也不知道到底错在了哪
1 个解决方案
#1
既然是java.lang.NullPointerException,看看错误信息提示你到底是哪行报的错误,然后看看那行代码什么为null的对象调用其属性或者方法。再检查是否该赋值的地方没有赋值或者是哪里的逻辑错误导致的
#1
既然是java.lang.NullPointerException,看看错误信息提示你到底是哪行报的错误,然后看看那行代码什么为null的对象调用其属性或者方法。再检查是否该赋值的地方没有赋值或者是哪里的逻辑错误导致的