我正在使用NetBeans 8.0上的java swing。當我運行此代碼時,我有重複的消息。最後我評論它。之後,系統不停止工作。最後我使用任務管理器並停止整個系統。然後我有Java結果:1錯誤。
這是我的源代碼。
如何停止Java結果:1錯誤?爲什麼會發生?
public void SaveDamage(JTextField txtItID, JTextField txtInID, JTextField txtQty, JTextField txtTotal) {
try {
ResultSet rs1 = JDBC.getData("select inid from damage where inid='" + txtInID.getText() + "'");
if (rs1.next()) {
JOptionPane.showMessageDialog(null, "This invoice is already finalized", "Error", JOptionPane.ERROR_MESSAGE);
} else {
try {
boolean bool = JDBC.putData("insert into damage(itid, qty,total, inid) values('" + txtItID.getText() + "' , '" + txtQty.getText() + "' ,'"+txtTotal.getText()+"' , '" + txtInID.getText() + "')");
if (bool) {
JOptionPane.showMessageDialog(null, "damaged saved");
try {
ResultSet rs = JDBC.getData("select avqty from item where itid='" + txtItID.getText() + "' ");
while (rs.first()) {
int newqty = rs.getInt("avqty") - Integer.valueOf(txtQty.getText());
try {
boolean bool1 = JDBC.putData("update item set avqty = '" + newqty + "' where itid = '" + txtItID.getText() + "' ");
if (bool1) {
// JOptionPane.showMessageDialog(null, txtItID.getText());
// JOptionPane.showMessageDialog(null, "item updated");
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
這是我的JDBC類。
package Model;
//import static Model.JDBC.con;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class JDBC {
static String driver = "com.mysql.jdbc.Driver";
static String url = "jdbc:mysql://localhost/cat";
static Connection con;
static boolean b ;
public static void setCon() {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, "root", "access456");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
public static Connection getCon() throws Exception {
if (con == null) {
setCon();
}
return con;
}
public static boolean putData(String sql) {
try {
PreparedStatement state = getCon().prepareStatement(sql);
state.executeUpdate();
b = true;
} catch (Exception ex) {
b= false;
JOptionPane.showMessageDialog(null, ex);
}
return b;
}
public static ResultSet getData(String sql) throws Exception {
Statement state = JDBC.getCon().createStatement();
ResultSet rset = state.executeQuery(sql);
return rset;
}
}
我相信你可以簡化這些'try'-'catch'語句到一個塊中,因爲catch是相同的。 –