繁体   English   中英

使用JOptionPane更新mysql

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM