这段代码告诉我:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String UI=null;
String sql="select * from Outcome";
try {
sta=con.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
try {
rs=sta.executeQuery(sql);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
try {
while(rs.next()){
UI=rs.getString("OutID");
if(UI.equals(jTextField5.getText())){
System.out.println("\n"+UI);
UI=jTextField5.getText();
String Des=jTextField6.getText();
String Mo=jTextField7.getText();
String Da=String.valueOf(jComboBox4.getSelectedItem())+"/"+String.valueOf(jComboBox5.getSelectedItem())+"/"+String.valueOf(jComboBox6.getSelectedItem());
String Not=jTextField8.getText();
String st = String.format("UPDATE Outcome SET OutID = '%s', Description = '%s', MoneyAmount = '%s', ODate = '%s', Notes = '%s'",UI,Des,Mo,Da,Not);
sta.executeUpdate(st);
// ps.setString(1, jTextField5.getText());
//ps.setString(2,jTextField6.getText());
// ps.setString(3,jTextField7.getText());
//ps.setString(4,String.valueOf(jComboBox4.getSelectedItem()+"/"+String.valueOf(jComboBox5.getSelectedItem())+"/"+String.valueOf(jComboBox6.getSelectedItem())));
sta.close();
con.close();
}
}
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
例外:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] حالة رأس المؤشر غير صالحة
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.JdbcOdbcResultSet.getString(Unknown Source)
at comcentrt.Outcomes.jButton4ActionPerformed(Outcomes.java:861)
at comcentrt.Outcomes.access$3(Outcomes.java:846)
at comcentrt.Outcomes$5.actionPerformed(Outcomes.java:380)
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.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)
答案 0 :(得分:1)
您已关闭while循环内的连接
con.close();
这就是你得到例外的原因。 在while循环之后关闭它
while(rs.next())
{
// your code
}
con.close();
答案 1 :(得分:1)
有一些问题,但让我们开始讨论可能导致问题的原因......
while(rs.next()){
UI=rs.getString("OutID");
if(UI.equals(jTextField5.getText())){
System.out.println("\n"+UI);
//...
sta.executeUpdate(st);
// This probably won't help...
sta.close();
// This will definitely not help...
con.close();
}
}
您正在关闭生成Statement
的基础ResultSet
,但更重要的是,您正在关闭实际Connection
,因此下次您致电{{1}时},rs.next
将被报告为已关闭...
也...
ResultSet
将关闭之前的String st = String.format("UPDATE Outcome SET OutID = '%s', Description = '%s', MoneyAmount = '%s', ODate = '%s', Notes = '%s'",UI,Des,Mo,Da,Not);
sta.executeUpdate(st);
(感谢Mark Rotteveel)。如果JDBC驱动程序和数据库支持,您可以使用ResultSet
方法之一,例如......
Result#updateXxx
请查看Retrieving and Modifying Values from Result Sets了解更多详情......
如果JDBC驱动程序/数据库不支持此功能,则需要创建一个新的rs.update("OutID", UI);
来执行操作。
另外,以这种方式格式化Statement
并以这种方式在数据库语句中使用它......
String
真的非常危险。它让您容易受到SQL注入攻击。相反,您应该使用Prepared Statements
此外,您的资源管理还不够理想......
看看这个......
String st = String.format("UPDATE Outcome SET OutID = '%s', Description = '%s', MoneyAmount = '%s', ODate = '%s', Notes = '%s'",UI,Des,Mo,Da,Not);
如果try {
sta=con.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
try {
rs=sta.executeQuery(sql);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
抛出异常会怎样?现在,当您尝试拨打createStatement
NullPointerException
看看这个......
executeQuery
如果在致电try {
while(rs.next()){
if(UI.equals(jTextField5.getText())){
sta.close();
con.close();
}
}
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
之前抛出异常会怎样?在这样的close
语句中调用close
是非常危险的,因为它可能意味着这些资源永远不会被关闭。
相反,你可以使用Java 7 try-catch
...
try-with-resources
请查看The try-with-resources Statement了解详情
或者,如果您不使用Java 7,则必须按照旧学校的方式进行,并使用try (Statement sta = con.createStatement()) {
try (ResultSet rs = sta.executeQuery(sql)) {
while (rs.next()) {
UI = rs.getString("OutID");
if (UI.equals(jTextField5.getText())) {
System.out.println("\n" + UI);
UI = jTextField5.getText();
String Des = jTextField6.getText();
String Mo = jTextField7.getText();
String Da = String.valueOf(jComboBox4.getSelectedItem()) + "/" + String.valueOf(jComboBox5.getSelectedItem()) + "/" + String.valueOf(jComboBox6.getSelectedItem());
String Not = jTextField8.getText();
String st = String.format("UPDATE Outcome SET OutID = '%s', Description = '%s', MoneyAmount = '%s', ODate = '%s', Notes = '%s'", UI, Des, Mo, Da, Not);
sta.executeUpdate(st);
}
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
try-catch-finally
在任何一种情况下都可以保证,无论发生什么(JVM在执行中死亡),都会尝试关闭资源。
答案 2 :(得分:-3)
Dataconnection dbconnect = new Dataconnection();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = dbconnect.getConnection();
ps = con.prepareStatement("query");
rs = ps.executeQuery();
while(rs.next())
{
}
} catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
} finally
{
try {
if (rs != null)
{
rs.close();
rs = null;
}
if (ps != null)
{
ps.close();
ps = null;
}
if (con != null)
{
con.close();
con = null;
}
} catch (Exception ee)
{
System.out.println(ee.getMessage());
ee.printStackTrace();
}