简体   繁体   中英

updating mysql using JOptionPane

I am trying to update my mysql table using JoptionPane. So the idea is to as for the id to update first then check if it exit then, ask for new value to replace it with.

here is the source for the line

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) {
        }
    }

Use PreparedStatement instead of Statement as your code is vulnerable to SQL injection

I think this is what you need :

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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