![](/img/trans.png)
[英]Java program to connect azure database in Netbeans I'm getting error
[英]I'm trying to update my database with java . i'm working in netbeans , there no error in the code but still the rows are not updated
我正在嘗試使用Java更新數據庫。 我在Netbeans中工作,代碼中沒有錯誤,但是行沒有更新。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(ClassNotFoundException | InstantiationException | IllegalAccessException e){
JOptionPane.showMessageDialog(this,"Error in connectivity" );
}
try{
String m11 = jTextField1.getText();
String m22 = jTextField2.getText();
com.mysql.jdbc.Connection conn = (com.mysql.jdbc.Connection)DriverManager.getConnection("jdbc:mysql://localhost/inventorysystem","root","root123");
Statement stmt = conn.createStatement();
int bp = JOptionPane.showConfirmDialog(this,"Do you want to update the record ?");
if(bp == JOptionPane.YES_OPTION){
String query = "update inventorycatalogmap set inventorycatalogname = '"+m11+"' and ProductCatalog='"+m22+"' where inventorycatalogname='"+m11+"';";
stmt.execute(query);
JOptionPane.showMessageDialog(this,"Record has been updated");
}
if(bp == JOptionPane.CANCEL_OPTION){
NewFrame2 t = new NewFrame2();
t.dispose();
t.setVisible(true);
}
if(bp == JOptionPane.NO_OPTION){
NewFrame2 i = new NewFrame2();
i.dispose();
i.setVisible(true);
}
stmt.close();
conn.close();
}
catch(SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null,"Invalid Entry","message",2);
}
}
任何人都可以建議,我的實現中出了什么問題?
您缺少conn.commit();
stmt.close();
conn.commit();
conn.close();
可能您的連接的自動提交未設置為true。
您的代碼易於出現語法錯誤和SQL注入,而您必須使用PreparedStatement
它更安全,更有用:
String query = "update inventorycatalogmap set inventorycatalogname = ?, ProductCatalog=? where inventorycatalogname=?";
try (PreparedStatement update = conn.prepareStatement(query)) {
update.setString(1, m11);
update.setString(2, m22);
update.setString(3, m11);
update.executeUpdate();
}
您的查詢有錯誤:
set inventorycatalogname = '" + m11 + "' and ProductCatalog='" + m22 + "' where
//---------------------------------------^^^
同樣,您不應該使用它, and
當您設置許多字段時,
只需將它們分開即可,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.