java tutorial - Prepared statement java - java programming - learn java - java basics - java for beginners
PreparedStatement interface
- The PreparedStatement interface is the sub interface of the Statement.
- It is used to execute parameterized query.
Parameterized query
String sql="insert into wikitechy_employee values(?,?,?)";
click below button to copy the code. By - java tutorial - team
- You can see, you are passing parameter (?) for the values.
- Its value can be a set by calling the setter methods of PreparedStatement.
Why we use PreparedStatement in java ?
- Improves performance: The PreparedStatement is faster in application, if we use PreparedStatement interface, the query is compiled only one time.
How to get the instance of PreparedStatement ?
- The prepareStatement() method of Connection interface is used to return the object of PreparedStatement.
Syntax
public PreparedStatement prepareStatement(String query)throws SQLException{}
click below button to copy the code. By - java tutorial - team
Methods of PreparedStatement interface
Method | Description |
---|---|
public void setInt(int paramIndex, int value) | sets the integer value to the given parameter index. |
public void setString(int paramIndex, String value) | sets the String value to the given parameter index. |
public void setFloat(int paramIndex, float value) | sets the float value to the given parameter index. |
public void setDouble(int paramIndex, double value) | sets the double value to the given parameter index. |
public int executeUpdate() | executes the query. It is used to create, drop, insert, update, delete and so on. |
public ResultSet executeQuery() | executes the select query. It returns an instance of ResultSet. |
Sample Code
- In this example we will execute a SQL statement using PreparedStatement object.
- In this example we will use "INSERT INTO" SQL statement.
- An INSERT INTO statement is used to insert the value into the database table.
- In this example we will first create a database table using MySQL and then we will create a Java classes into which we will use the classes and interfaces of java.sql package for making connection with database and to insert value into the database table.
Database table
Wikitechy_Employee
CREATE TABLE `Wikitechy_Employee` (
`Emp_Id` varchar(15) NOT NULL,
`Emp_Name` varchar(20) DEFAULT NULL,
`Emp_Add` varchar(35) DEFAULT NULL,
`Salary` int(10) DEFAULT NULL,
PRIMARY KEY (`Emp_Id`)
) ENGINE=InnoDB
click below button to copy the code. By - java tutorial - team
PreparedStatementExample.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class PreparedStatementExample
{
public static void main(String args[])
{
String driver="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/record";
String user = "root";
String password = "root";
Connection con = null;
PreparedStatement ps = null;
try
{
Class.forName(driver);
}
catch(ClassNotFoundException c)
{
System.out.println(c);
}
try
{
con = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO Wikitechy_Employee(Emp_Id, Emp_Name, Emp_Add,Salary) VALUES(?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, "01278");
ps.setString(2, "Shym Andrews");
ps.setString(3, "67,xxxxxxxx.");
ps.setString(4, "700000");
int i = ps.executeUpdate();
if(i >0)
{
System.out.println("\nData inserted into table successfully");
}
else
System.out.println("Data can't be inserted");
}
catch(SQLException sqe)
{
sqe.printStackTrace();
}
}
}