In this tutorial you will learn how to create a Statement object in JDBC
In this tutorial you will learn how to create a Statement object in JDBCJDBC Statement is an interface of java.sql.*; package. It is used for execution of SQL statements. It returns a ResultSet object. This result set object contains the result of the query. Statement interface provides basic method for SELECT, INSERT, UPDATE, DELETE operations in the database. Its query is compiled every time when request is made, therefore is little slower in comparison to PreparedStatement.
To create a Statement object you first need to create Connection object because statement object takes a reference to connection object.
Example-
Connection connection=DeriverManager.getConnection("ConectionUrl","userName","passWord"); // Creating connection object
Statement stmt=connection.createStatement(); // Creating statement
After the use of connection object you should close() these objects by calling a close method.
statement.close();
connection.close();
A simple example given below, which illustrates the creation of connection object and closing it after it is used.
At first create table named student in MySql database and inset values into it as.
CREATE TABLE student (
RollNo int(9) PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text
);
Insert Value Into student table
INSERT INTO student VALUES(1, 'Ram', 'B.Tech', 'Delhi') ;
JDBCStatementObjectExample.java
package roseindia.net; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCStatementObjectExample { public static void main(String[] args) throws SQLException { Connection connection = null; // connection reference variable for getting // connection Statement statement = null; // Statement reference variable for query // Execution ResultSet resultSet = null; // ResultSet reference variable for saving query // result String conUrl = "jdbc:mysql://localhost:3306/"; String driverName = "com.mysql.jdbc.Driver"; String databaseName = "student"; String usrName = "root"; String usrPass = "root"; try { // Loading Driver Class.forName(driverName); } catch (ClassNotFoundException e) { System.out.println(e.toString()); } try { // Getting Connection connection = DriverManager.getConnection(conUrl + databaseName, usrName, usrPass); // Getting reference to connection object statement = connection.createStatement(); // creating Query String String query = "SELECT * FROM student"; // excecuting query resultSet = statement.executeQuery(query); while (resultSet.next()) { // Didplaying data of tables System.out.println("Roll No " + resultSet.getInt("RollNo") + ", Name " + resultSet.getString("Name") + ", Course " + resultSet.getString("Course") + ", Address " + resultSet.getString("Address")); } } catch (Exception e) { System.out.println(e.toString()); } finally { // Closing connection resultSet.close(); statement.close(); connection.close(); } } }When you run this application it will display message as shown below:
Roll No 1, Name Ram, Course B.Tech, Address Delhi |