public class JDBC_Pro extends HelloWorld {
//Database essential
public String JDBC_Driver = "com.mysql.jdbc.Driver";
public String DB_Url = "JDBC:mysql://localhost:3306/kalinga";
public String USER = "root";
public String pass = "Welcome123";
public Connection con = null ;
public JDBC_Pro(){
JDBC_Driver = "com.mysql.jdbc.Driver";
DB_Url = "JDBC:mysql://localhost:3306/kalinga";
USER = "root";
pass = "Welcome123";
try{
Class.forName(JDBC_Driver);
//System.out.println("Connecting to Database");
con = DriverManager.getConnection(DB_Url, USER, pass);
}
catch(Exception e){System.out.println(e);
}
}
public void InsertData(String State,String City,String Age,String Dep,String Person,String Amount,String Des){
try{
String sql ="insert into kan values(?,?,?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,State);
pst.setString(2, City);
pst.setString(3, Age);
pst.setString(4, Dep);
pst.setString(5, Person);
pst.setString(6, Amount);
pst.setString(7, Des);
int numRowsChanged = pst.executeUpdate();
System.out.println(numRowsChanged);
pst.close();
con.close();
}catch(Exception e){ e.printStackTrace();}
}
Abov是我的JDBC文件...我想从我的servlet文件中调用Method InsertData()。当我单独运行JDBC文件时,数据会更新到表中,但是当我从我的servlet文件调用此方法时,如
JDBC_Pro b = new JDBC_Pro();
b.InsertData("the arguments goes here..");
异常
java.lang.ClassNotFoundException: com.mysql.jdbc.Driverjava.lang.NullPointerException
两个文件都在同一个包中! 任何人都可以解决这个问题
答案 0 :(得分:2)
您是否在项目中添加了 MySql连接器jar ?
我猜你错过了抛出异常的MySql连接器jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driverjava.lang.NullPointerException