0
我想連接java與mysql數據庫,但我得到一個空指針異常。mysql和java連接
注:我之前用這個方法,效果不錯
這是代碼:
public void OpenConnection() throws SQLException{
try {
try {
Class.forName("com.mysql.jdbc.Driver");
try {
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hema?"
+ "user=root&password=hima27890");
} catch (SQLException ex) {
System.out.print("Connection Not Found");
}
} catch (ClassNotFoundException ex) {
System.out.print("Not found Class");
}
} finally {
System.out.close();
}
}
,這是爲了在數據庫返回一些數據的方法:
public String[] GetData(String website) throws ClassNotFoundException, SQLException{
// System.out.println("\nGet data method");
// System.out.println("connection will start now");
OpenConnection();
System.out.println("connection works");
String queryget="SELECT * FROM hema.passwords WHERE website='"+website+"'";
Statement stm=(Statement) con.createStatement();
ResultSet rs;
rs = stm.executeQuery(queryget);
if(rs.next()){
System.out.println("if works");
arr[0]=rs.getString("email");
arr[1]=rs.getString("username");
arr[2]=rs.getString("password");
}
else{
System.out.println("The website doesn't exist");
}
System.out.println("method done");
return arr;
}
}
我把一些System.out.print知道錯誤在哪裏,它們都是分叉的,但不是最後一個「方法完成」。
,這是在調用的最後一個方法的JForm的代碼:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String Website = jTextField1.getText();
System.out.println("Website is "+Website);
Model model=new Model();
String arr2[]=model.GetData(Website);
System.out.println("App");
EmailText.setText(arr2[0]);
UserText.setText(arr2[1]);
PasswordText.setText(arr2[2]);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Passwords.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Passwords.class.getName()).log(Level.SEVERE, null, ex);
}
}
堆棧跟蹤:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Model.Model.GetData(Model.java:68)
at View.Passwords.jButton1ActionPerformed(Passwords.java:161)
at View.Passwords.access$200(Passwords.java:15)
at View.Passwords$3.actionPerformed(Passwords.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
生成成功(總時間:21秒),
注:我之前用這個方法,效果不錯 – 2014-10-11 03:53:58
可能重複:http://stackoverflow.com/questions/2591505/java-lang-classnotfoundexception-com-mysql-jdbc -driver – Nathan 2014-10-11 03:59:46
你有沒有添加mysql jar到你的項目? – 2014-10-11 04:18:14