java tutorial - jdbc statement | Statement interface - java programming - learn java - java basics - java for beginners
Statement interface
- Statement interface provides methods to execute queries with a database.
- It gives factory method to get the object of ResultSet.
Methods of Statement interface:
Method | Description |
---|---|
public ResultSet executeQuery(String sql) | It is used to execute SELECT query. It returns the object of ResultSet. |
public int executeUpdate(String sql) | It is used to execute specified query, it can be create, drop, insert, update, delete and so on. |
public boolean execute(String sql) | It is used to execute queries that may return multiple results. |
public int[] executeBatch() | It is used to execute batch of commands. |
Sample Code
//STEP 1. Import 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{
//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 = "UPDATE Employees set age=30 WHERE id=103";
// Let us check if it returns a true Result Set or not.
Boolean ret = stmt.execute(sql);
System.out.println("Return value is : " + ret.toString() );
// Let us update age of the record with ID = 103;
int rows = stmt.executeUpdate(sql);
System.out.println("Rows impacted : " + rows );
// Let us select all the records and display them.
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from 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 values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
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
}//end try
System.out.println("Thanks to wikitechy.com!");
}//end main
}//end JDBCExample
click below button to copy the code. By - java tutorial - team
- The above sample code compile we are using the code is,
C:\>javac JDBCExample.java
C:\>
click below button to copy the code. By - java tutorial - team
Output
C:\>java JDBCExample
Connecting to database...
Creating statement...
Return value is : false
Rows impacted : 1
ID: 001, Age: 20, First: John, Last: Stephen
ID: 002, Age: 24, First: Vincent, Last: hami
ID: 003, Age: 37, First: Harris, Last: Raj
ID: 004, Age: 30, First: William, Last: Jone
Thanks to wikitechy!