我正在使用我的应用程序连接数据库。 SELECT查询工作正常,这意味着建立连接。当我使用statement.execute(parameter)
方法时,我可以获取在服务器上保存的信息,但是当我尝试插入新数据并使用statement.executeUpdate(parameter)
方法时,会抛出我列出错误。好吧,我差不多3天就此问题卡住但发现没有任何与我的问题相关的帮助。
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid string or buffer length
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.buildTypeInfo(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Database.Database.InitializeDatabase(Database.java:14)
at Clients.Customer$2.actionPerformed(Customer.java:115)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Database.Database.executeQuery(Database.java:26)
at Clients.Customer.saveData(Customer.java:141)
at Clients.Customer$2.actionPerformed(Customer.java:116)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
非常特别,我可以看到我的问题在这里或可能不是。但是我无法弄清楚它为什么会给NullPointerException
。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Database.Database.executeQuery(Database.java:26)
at Clients.Customer.saveData(Customer.java:141)
这是我尝试执行查询的代码
public void saveData(){
if(!repOK()){
JOptionPane.showMessageDialog(null, "One or more mendetory fields are empty","Error",JOptionPane.WARNING_MESSAGE);
}
else{
database.executeQuery("INSERT INTO CustomerList(customerCode,customerName,resourcePerson,balance,mobile,landline) VALUES('"+customerCodeF.getText()+"','"+customerNameF.getText()+"','"+resourcePersonF.getText()+"','"+balanceF.getText()+"','"+mobileF.getText()+"','"+landLineF.getText()+"')");
JOptionPane.showMessageDialog(null,"Data Saved");
createCustomertable();
resetForm();
}
}
注意:此方法在保存按钮的actionListener中调用。
public void actionPerformed(ActionEvent e){
database.InitializeDatabase("Rimco");
saveData();
database.closeDatabase();
}
以下是我的Database类中用于执行查询的片段。
public void executeQuery(String query){
try {
//statement.executeUpdate(query);
PreparedStatement p= connection.prepareStatement(query);
p.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
首先,我尝试使用executeUpdate(query);
来完成工作但出现了同样的错误。然后我切换到PreparedStatment
但没有发生任何事情,同样的错误仍然存在。
任何想法都会有很大的帮助。 非常感谢!
答案 0 :(得分:0)
我猜你对文本字段的一个引用是null,所以当你这样做时
base.executeQuery("INSERT INTO CustomerList(customerCode,customerName,resourcePerson,balance,mobile,landline) VALUES('"+customerCodeF.getText()+"','"+customerNameF.getText()+"','"+resourcePersonF.getText()+"','"+balanceF.getText()+"','"+mobileF.getText()+"','"+landLineF.getText()+".....
你可能通过在一个你认为是对文本字段的引用的空值上调用getText()来抛出一个NPE。
您可能会发现在调试器中单步调试代码会很有帮助。
哦,你真的不应该将原始用户提供的文本输入连接到你的sql语句 - 而是将参数绑定到预准备语句。搜索“SQL Injection”或“Little Bobby Tables”以找出原因......