2017-02-18 40 views
-2

我正在一个简单的Java应用程序保存到MySQL数据库。它工作正常,直到黄线开始显示我的一些代码。然后记录不能再次保存在数据库中,而这是之前保存的。Java记录没有保存在mysql数据库与黄线

然后“con”和“Exception”开始显示一个黄色错误行。错误代码说我应该使用“尝试”的连接,我做了,仍然没有工作。请帮忙。由于

我的代码:

try { 
     Class.forName(driver); 
     Connection con = DriverManager.getConnection(url, user, pass); 
     String sql = "insert into biodatum " + "(firstname, lastname,  address, occupation, phone, email, image, gender, religion)" 
     + "values (?,?,?,?,?,?,?,?,?)"; 
     PreparedStatement pst = con.prepareStatement(sql); 
     //InputStream is = new FileInputStream(new File(s)); 
     pst.setString(1, firstname.getText()); 
     pst.setString(2, lastname.getText()); 
     pst.setString(3, address.getText()); 
     pst.setString(4, occupation.getText()); 
     pst.setString(5, phone.getText()); 
     pst.setString(6, email.getText()); 
     pst.setBytes(7, image_person); 
     pst.setString(8, gender.getSelectedItem().toString()); 
     pst.setString(9, religion.getSelectedItem().toString()); 



     JOptionPane.showMessageDialog(this, "Record Saved"); 
     firstname.setText(""); 
     lastname.setText(""); 
     address.setText(""); 
     occupation.setText(""); 
     phone.setText(""); 
     email.setText(""); 
     image_path.setText(""); 

     pst.close(); 
     con.close(); 

    } 
    catch (Exception e){ 
     JOptionPane.showMessageDialog(this, e.getMessage()); 
    } 
} 
+0

“黄线”你的意思是eclipse正在向你显示资源警告? –

+0

不,我正在使用,NetBeans IDE,YES,资源警告 – masquellett

+0

所以它告诉你使用[try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose。 HTML)。但是,如果这不能解决它,这是你的问题是无关的(obv)。这只是简洁的编码,因为它确保连接关闭。 –

回答

0

什么是执行你的预处理语句?

如果你的代码工作过,那么你无意中删除了一些重要的东西,比如执行准备好的语句的代码行,它应该看起来是这样的:

pst.executeUpdate(); 

所以,你的代码应该是这样的:

.................................. 
.................................. 
String sql = "INSERT INTO biodatum " + "(firstname, lastname, address, occupation, phone, email, image, gender, religion) " 
     + " VALUES (?,?,?,?,?,?,?,?,?)"; 
PreparedStatement pst = con.prepareStatement(sql); 
pst.setString(1, firstname.getText()); 
pst.setString(2, lastname.getText()); 
pst.setString(3, address.getText()); 
pst.setString(4, occupation.getText()); 
pst.setString(5, phone.getText()); 
pst.setString(6, email.getText()); 
pst.setBytes(7, image_person); 
pst.setString(8, gender.getSelectedItem().toString()); 
pst.setString(9, religion.getSelectedItem().toString()); 
pst.executeUpdate(); // ************ HERE *************** 
.................................. 
.................................. 
+0

非常感谢,这是真的,我想我错误地删除了该行。 – masquellett

+0

它不时发生;) – DevilsHnd

相关问题