type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:498)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause java.lang.NullPointerException
org.apache.jsp.articleDetailFlat_jsp._jspService(articleDetailFlat_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
<%
String strId = request.getParameter("id");
if(strId == null || strId.trim().equals("")){
out.println("Error ID!");
return ;
}
int id = 0;
try{
id = Integer.parseInt(strId);
}catch(NumberFormatException e){
out.print("Error ID again!");
return;
}
List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();
String sql = "select * from article where id = " + id + "order by pdate asc";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
while(rs.next()){
Article a = new Article();
a.initFromRs(rs);
articles.add(a);
}
DB.close(rs);
DB.close(stmt);
DB.close(conn); %>
Connection conn = DB.getConn();
得不到连接,以致后面的createStatement报空指针错误
你的DB类中的这几个方法是怎么写的
String strId = request.getParameter("id");
DB类的代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import com.mysql.jdbc.PreparedStatement;public class DB {
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root" );
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStmt(Connection conn){
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery( Statement stmt,String sql){
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static PreparedStatement prepareStmt(Connection conn,String sql){
PreparedStatement pstmt = null;
try{
pstmt = (PreparedStatement) conn.prepareStatement(sql);
}catch(SQLException e){
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepareStmt(Connection conn,String sql,int autoGeneratedKeys){
PreparedStatement pstmt = null;
try{
pstmt = (PreparedStatement) conn.prepareStatement(sql,autoGeneratedKeys);
}catch(SQLException e){
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void close(Statement stmt){
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
public static void close(ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}