Home Tutorial Java Jdbc JDBC Prepared Statement Example

 
 

JDBC Prepared Statement Example
Posted on: October 16, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn about java.sql.PreparedStatement interface and how to use it in your application

JDBC Prepared Statement

java.sql.PreparedStatement is enhanced version of java.sql.Statement.It inherits some functionality of java.sql.Statement and also add some extra feature to it. In is an enhanced version of statement which allows precompiled queries with parameter. It does not compile the query every time, The query once compiled is used every time. PreparedStatement object can also be used with SQL statement with no parameter.

Creating a java.sql.PreparedStamenet

PreparedStatement ptmt=con.prepareStatement(" UPDATE StudentDetail SET Name=? WHERE RollNo=?");

This ptmt object contains the UPDATE StudentDetail SET Name=? WHERE RollNo=? which have already sent to the DBMS and is prepared for execution.

Now we pass the parameter to ptmt object as

ptmt.setString(2,"John");

This sets the String "John" to the second column of the StudentDetail table.

An example given below which illustrates how to update the table using prepared statement.

At first create a database in MySql named student and then create a table StudentDetail into it

CREATE TABLE student (
RollNo int(9)  PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text
 );

Java code for Prepared Statement

JDBCPreparedStatementExample.java

package roseindia.net;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCPreparedStatementExample {
	Connection connection = null;

	public JDBCPreparedStatementExample() {
		try {
			// Loading the driver
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
	}

	public Connection createConnection() {
		Connection con = null;
		if (connection != null) {
			System.out.println("Cant create a connection");
		} else {
			try {
				// Crating a Connection to the Student database
				con = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/student", "root",
						"root");
				System.out.println("Connection created Successfully");
			} catch (SQLException e) {
				System.out.println(e.toString());
			}
		}
		return con;
	}

	public static void main(String[] args) throws SQLException {
		JDBCPreparedStatementExample jdbccOnnectionExample = new JDBCPreparedStatementExample();
		Connection conn = jdbccOnnectionExample.createConnection();
		// Creating a Statement reference variable
		PreparedStatement ptmt = null;
		// getting the connection reference to the Statement
		String queryString = "UPDATE student SET Name=? WHERE RollNo=?";
		// getting reference of connection
		ptmt = conn.prepareStatement(queryString);
		// setting parameters to prepared statement
		ptmt.setString(1, "John");
		ptmt.setInt(2, 2);
		// Executing query
		ptmt.executeUpdate();
		System.out
				.println("Table Updated successfully Using prepared statement............");
		ptmt.close();
		conn.close();
	}
}
When you run this application it will display message as shown below:

Connection created Successfully
Table Updated successfully Using prepared statement............

Download this example code

Advertisement

Related Tags for JDBC Prepared Statement Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 16, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com