简体   繁体   中英

Updating existing Row on database jdbc

No error is showing when i click the button but the table on the database doesn't update.

String heh = jLabel17.getText();
try {
    stmt.executeUpdate("UPDATE books SET availability='"+"Unavailable"+"' where Book_title='"+heh+"'");
}catch (SQLException err) {
    System.out.println(err.getMessage() );
}

You have messed up the query totally,

stmt.executeUpdate("UPDATE books SET availability='"+"Unavailable"+"' where Book_title='"+heh+"'");

should be,

stmt.executeUpdate("UPDATE books SET availability='Unavailable' where Book_title='"+heh+"' ");

It is advisable to print query before you execute , as that avoids common mistakes. Also try to use Prepared Statements as yours is vulnerable to sql injection

Read this Prepared Statements and JDBC Drivers

AFTER HOURS OF RESEARCH, I FOUND THE SOLUTION, I REPLACED THIS

String heh = jLabel17.getText();

    try{
        stmt.executeUpdate("UPDATE books SET availability='"+"Unavailable"+"' where Book_title='"+heh+"'");
        }catch(SQLException err){
        System.out.println(err);
   }

WITH THIS CODE

String heh = jLabel17.getText();

    try{
        con = DriverManager.getConnection("jdbc:derby://localhost:1527/Dafuq7","Dafuq7","Dafuq7");
                    // Creating Statement for query execution
                    stmt = con.createStatement();
                    // creating Query String
                    String query = "UPDATE books SET availability='NOT AVAILABLE' WHERE book_title='"+heh+"'";
                    // Updating Table
                    int rows = stmt.executeUpdate(query);
                    System.out.println(rows + " Rows Updated Successfully....");
   } catch (Exception e) {
        System.out.println(e.toString());
   }

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