[英]updating mysql using JOptionPane
我正在嘗試使用JoptionPane更新我的mysql表。 因此,想法是先更新ID,然后檢查是否退出,然后要求新值替換它。
這是該行的來源
else if(ae.getSource()==updateVid){
String id =JOptionPane.showInputDialog("Enter ID you want to update:");
try {
con=DriverManager.getConnection(url,"root","success123");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
int numRows = stmt.executeUpdate("UPDATE FROM films WHERE id='"+id+"'");
String ids =JOptionPane.showInputDialog("ID:");
String fn=JOptionPane.showInputDialog("Title:");
String sn =JOptionPane.showInputDialog("Description:");
String tn =JOptionPane.showInputDialog("Year:");
String frn =JOptionPane.showInputDialog("Art:");
String fifn =JOptionPane.showInputDialog("Duration:");
rs = stmt.executeQuery("SELECT * FROM films");
JOptionPane.showMessageDialog(null,"Video has been Rented to the Out","New Rent" ,JOptionPane.OK_OPTION);
while (rs.next()) {
}
rs.close();
stmt.close();
con.close();
}
catch (SQLException sqle) {
}
}
使用PreparedStatement
而不是Statement
因為您的代碼容易受到SQL注入的攻擊
我認為這是您需要的:
String updateQuery = "UPDATE films set title = ?,description=?,year=?,art=?,duration=? where id=?";
PreparedStatement ps = con.prepareStatement(updateQuery);
String fn=JOptionPane.showInputDialog("Title:");
String sn =JOptionPane.showInputDialog("Description:");
String tn =JOptionPane.showInputDialog("Year:");
String frn =JOptionPane.showInputDialog("Art:");
String fifn =JOptionPane.showInputDialog("Duration:");
ps.setString(1,fn);
ps.setString(2,sn);
ps.setString(3,tn);
ps.setString(4,frn);
ps.setString(5,fifn);
ps.setString(6,id);
int numRows = ps.executeUpdate();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.