- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Statement in JDBC?
The Statement interface represents the static SQL statement, it is used to create and execute general purpose SQL statements using Java programs.
Creating a statement
You can create an object of this interface using the createStatement() method of the connection interface. Create a statement by invoking this method as shown below.
Statement stmt = null; try { stmt = conn.createStatement( ); . . . } catch (SQLException e) { . . . } finally { . . . }
Executing the Statement object
Once you have created the statement object you can execute it using one of the execute methods namely, execute(), executeUpdate() and, executeQuery().
execute(): This method is used to execute SQL DDL statements, it returns a boolean value specifying weather the ResultSet object can be retrieved.
executeUpdate(): This method is used to execute statements such as insert, update, delete. It returns an integer value representing the number of rows affected.
executeQuery(): This method is used to execute statements that returns tabular data (example SELECT statement). It returns an object of the class ResultSet.
Example
Following JDBC application demonstrates how to create and, execute a statement.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class StatementExample { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/testdb"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Executing the statement String createTable = "CREATE TABLE Employee(“ + "Name VARCHAR(255), " + "Salary INT NOT NULL, " + "Location VARCHAR(255))"; boolean bool = stmt.execute(createTable); String insertData = "INSERT INTO Employee(" + "Name, Salary, Location) VALUES " + "('Amit', 30000, 'Hyderabad'), " + "('Kalyan', 40000, 'Vishakhapatnam'), " + "('Renuka', 50000, 'Delhi'), " + "('Archana', 15000, 'Mumbai')"; int i = stmt.executeUpdate(insertData); System.out.println("Rows inserted: "+i); ResultSet rs = stmt.executeQuery("Select *from Employee"); while(rs.next()) { System.out.print("Name: "+rs.getString("Name")+", "); System.out.print("Salary: "+rs.getInt("Salary")+", "); System.out.print("City: "+rs.getString("Location")); System.out.println(); } } }
Output
Connection established...... Rows inserted: 4 Name: Amit, Salary: 30000, City: Hyderabad Name: Kalyan, Salary: 40000, City: Vishakhapatnam Name: Renuka, Salary: 50000, City: Delhi Name: Archana, Salary: 15000, City: Mumbai
- Related Articles
- What is the use of setFetchSize() and setMaxRows() methods of the JDBC Statement Interface?
- What is JDBC?
- What is CLOSE_CURSORS_AT_COMMIT in JDBC?
- What is PreparedStatement in JDBC?
- What is CallableStatement in JDBC?
- What is ResultSet holdability in JDBC?
- What is batch processing in JDBC?
- What is Type_FORWARD_ONLY ResultSet in JDBC?
- What is TYPE_SCROLL_INSENSITIVE ResultSet in JDBC?
- What is TYPE_SCROLL_SENSITIVE ResultSet in JDBC?
- What is ResultSet Concurrency in JDBC?
- What is DatabaseMetaData in JDBC? What is its significance?
- What is ResultSetMetaData in JDBC? What is its significance?
- What is a CachedRowSet in JDBC? Explain?
- What is RowId object in JDBC Explain?
