i'm a novice programmer and following is a part of my code to insert data into the database whose name is book_details.
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "INSERT INTO issue_details VALUES("+BookIDvar+",'"+StudentIDvar+"','"+StudentNamevar+"')";
stmt.executeUpdate(sql);
sql = "INSERT INTO book_details (book_status) VALUES ('notavailable') WHERE book_id = "+BookIDvar+"";
// there are 4 fields in the database. I wish to insert the string 'notavailable' into the 4th field which is named book_status for a particular BookID
stmt.executeUpdate(sql);
//STEP 6: Clean-up environment
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}
Here i intend to update the status of a library book available and not available. I prefer non - Boolean value for some other reasons.
After the execution the book_details database is not updated. It also gives an error
MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
.
Is there something wrong with the Sql statement or is it something else? Other operations on the same database in other locations are working fine.