求帮助,jsp使用Java类时出现
空指针错误,谢谢!
<%@page import="java.sql.*"%>
<%@page import="servlet.DBConnect"%>
<% DBConnect db=new DBConnect();
ResultSet result=db.executeQuery("select * from mapdata");
%>
然后,下面是DBConnect类代码
package servlet;
import java.sql.*;
public class DBConnect
{
public static final String driver="com.mysql.jdbc.Driver";
public static final String url="jdbc:mysql://localhost:3306/mydata";
public static final String user="root";
public static final String password="2224101360";
ResultSet resultset=null;
PreparedStatement statement=null;
Connection conn=null;
public Connection getConnection()
{ try{Class.forName(driver);
return DriverManager.getConnection(url,user,password);
}
catch(
ClassNotFoundException e){System.out.println("Driver not found");return null;}
catch(
SQLException e){System.out.println("Error:"+e.getMessage());return null;}
}
public ResultSet executeQuery(String sql) throws Exception
{
conn=this.getConnection();
statement=conn.prepareStatement(sql);
return resultset=statement.executeQuery();
}
public int executeUpdate(String sql)
{
try{
conn=this.getConnection();
statement=conn.prepareStatement(sql);
return statement.executeUpdate();
}catch(Exception e){System.out.println("Error:"+e.getMessage());return 0;}
}
public void closeAll() throws Exception
{
if(null!=resultset) resultset.close();
if(null!=statement) statement.close();
if(null!=conn) conn.close();