Creating JDBC Statement object

In this tutorial you will learn how to create a Statement object in JDBC


Share on Google+Share on Google+

Creating JDBC Statement object


In this tutorial you will learn how to create a Statement object in JDBC

Creating JDBC Statement Object

JDBC 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.


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.



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 (
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') ;


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
		} catch (ClassNotFoundException e) {
		try {
			// Getting Connection
			connection = DriverManager.getConnection(conUrl + databaseName, usrName,
			// Getting reference to connection object
			statement = connection.createStatement();
			// creating Query String
			String query = "SELECT * FROM student";
			// excecuting query
			resultSet = statement.executeQuery(query);
			while ( {
				// 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) {
		} finally {
			// Closing connection
When you run this application it will display message as shown below:

Roll No 1, Name Ram, Course B.Tech, Address Delhi

Download this example code


Posted on: October 21, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles