2015-04-01 102 views
-1
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文件一樣無法從Servlet文件中調用JDBC文件中的方法

JDBC_Pro b = new JDBC_Pro(); 
b.InsertData("the arguments goes here.."); 

異常

java.lang.ClassNotFoundException: com.mysql.jdbc.Driverjava.lang.NullPointerException 

兩個文件都在同一個包! 任何人都可以請解決這個問題

+0

MySQL連接罐子看看http://stackoverflow.com/questions/15827547/java-lang-classnotfoundexceptioncom-mysql-jdbc-driver – svenhornberg 2015-04-01 08:48:13

+0

將'mysql-connector-java - *******。jar'添加到你的lib路徑 – 2015-04-01 08:49:22

回答

1

您是否在您的項目中添加了MySql連接器jar

我猜你缺少哪個拋出異常

java.lang.ClassNotFoundException: com.mysql.jdbc.Driverjava.lang.NullPointerException 
+0

謝謝大家..它已經修復了,我將mysql連接器添加到lib文件夾 – 2015-04-02 17:18:07