English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The following is an example that uses the following three queries and open and close statements-
boolean execute(String SQL)Returns a boolean value true if the result set object can be retrieved; otherwise returns false. Use this method to execute SQL DDL statements, or use it when real dynamic SQL is needed.
int executeUpdate(String SQL)Returns the number of rows affected by the SQL statement. Use this method to execute SQL statements for which you want to get the number of affected rows, such as INSERT, UPDATE, or DELETE statements.
ResultSet executeQuery(String SQL)Returns a ResultSet object. Use this method when you want to get a result set, just like using a SELECT statement.
This example code is written based on the environment and database settings in the previous chapters.
Copy and paste the following example into JDBCExample.java, as follows compile and run:
//step1Import the required packages import java.sql.*; public class JDBCExample { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //step2Register the JDBC driver program Class.forName("com.mysql.jdbc.Driver"); //step3:establish connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //step4:execute query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql = "UPDATE Employees set age=30 WHERE id=103"; // Let's check if it returns a real result set. Boolean ret = stmt.execute(sql); System.out.println("Return value is : " + ret.toString()); // Let's update ID = 103of the records' age; int rows = stmt.executeUpdate(sql); System.out.println("Rows impacted : " + rows ); // Let's select all records and display them. sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); //step5:extract data from the result set while(rs.next()){ //retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //display value System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } //step6:clean up environment rs.close(); catch(SQLException se conn.close(); }catch(SQLException se){ //Handle JDBC error se.printStackTrace(); }catch(Exception e){ //Handle the error of Class.forName e.printStackTrace(); }finally{ //for closing resources try{ stmt.close(); catch(SQLException se }2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); }//End main }//End JDBCExample
Now let's compile the above example as follows:
C:\>javac JDBCExample.java C:\>
at runtimeJDBCExample,it will produce the following result-
C:\>java JDBCExample Connecting to database... Creating statement... Return value is: false Rows impacted : 1 ID: 100, Age: 18, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 30, First: Zaid, Last: Khan ID: 103, Age: 30, First: Sumit, Last: Mittal Goodbye! C:\>