how to check the return value of mysql update statement in JAVA


Problem :

I'm a novice java programmer and here is my code snippet to delete a record based on it's ID.

private void RemovebuttonActionPerformed(java.awt.event.ActionEvent evt) {   

   BookIDvar=Integer.parseInt(BookID.getText());
   System.out.println(BookIDvar);
   Connection conn = null;
   Statement stmt = null;
   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="DELETE FROM book_details WHERE book_id="+BookIDvar+"";
  stmt.executeUpdate(sql);
  //STEP 6: Clean-up environment

  stmt.close();
  conn.close();
 }

The code is working fine. But i want to check whether if there existed a record with that particular ID. Is there a way to check it using the current code itself? Does Java provide some constructs to check the return value of executUpdate similar to executeQuery?



Solution :

Answer

int a = stmt.executeUpdate(sql);

System.out.println(a); // ---> Number of columns the query has manipulated in the database

Syntax

int executeUpdate(String sql) throws SQLException

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

Note that the executeUpdate(); method return type is "int" i.e it returns the number of columns the query has manipulated in the database


Mysql Tutorials

Mysql Howto..

how to get top 5 amounts from mysql table?

How to store URLs in MySQL

how to test your mysql queries?

How to detemine if database server or embedded database is appropriate

How to retrieve a MySQL Database field with data between brackets {} as an array

How to connect to MySQL using PHP PDO in easyphp 16.1.1

How to insert partial data into a Mysql table?

How to Set up PHP Test Server in Dreamweaver?

How to get data of second and third table based of first table match

How to build unlimited level of menu through PHP and mysql

How to display an SQL string that has been constructed by MySQLDB cursor?

mysql best backup method? and how to dump backup to target directory

How to get “code first” to update tables in mysql?

How can I convert a column of date strings in a mysql db to UNIX timestamps?

Basic mySQL - Don't know how to show all results including 0

How to select the maximum value of a column in MySQL

How to create a simple mysql base treeview in php

How does database views keep the fast query speed when multiple FK-PK relationships among alll the tables [closed]

How to make login API in node.js using mysql database

How to suppress DDL log

How to check if $row['column_name'] is returning empty php mysql

mysql how to best set up tables where relation is one to several

How to join two tables and then display the sum value of each data of a table gathering from the other table

How can i get the system type of a value i get from a form/string?

how to read data from a MySQL server on (Mac Excel 2011 net.VB)? [closed]

How can i update a table using SQL Injection?

How load data from mySQL to JSON in EXTJS

How can you determine how much disk space a particular MySQL table is taking up?

How would I perform math within a sql query to calculate percent difference?

How to enforce constraints in mysql