数据库连接的小问题
package dao;import *;
import *;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* 加载驱动
*/
public class Driver extends HttpServlet {
/**
初始化数据库的驱动参数
*/
private String driverClass;
private String url;
private String user;
private String password;
@Override
public void init()
{
driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password= getInitParameter("password");
}
public String getDriverClass() {
return driverClass;
}
public String getUrl() {
return url;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class DBUtil {
Driver driver = new Driver();
private Connection conn = null;
DBUtil()
{
if(conn==null){
try {
Class.forName(driver.getDriverClass());
conn=DriverManager.getConnection(driver.getUrl(),driver.getUser(),driver.getPassword());
} catch (Exception ex) {
Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public Connection getConnection()
{
return conn;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import dao.DBUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class Test {
public static void main(String args[])
{
DBUtil db = new DBUtil();
try {
java.sql.PreparedStatement ps = db.getConnection().prepareStatement("insert into class(name) values (?)");
ps.setString(1, "张三");
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
为什么会插不进去 说有空指针