What is PreparedStatement?
It is an Interface.The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once.
Example of PreparedStatement in Java:
//Java Program of PreparedStatement
import java.sql.DriverManager;
import java.sql.*;
import java.util.Scanner;
class MyDatabaseConnection
{
Connection con;
Statement stmt;
PreparedStatement prstmt;
ResultSet rst;
ResultSetMetaData rsmd;
String server,mydatabase,user,password;
public MyDatabaseConnection(){
server="localhost";
mydatabase="demo";
user="root";
password="";
try {
con=DriverManager.getConnection("jdbc:mysql://" + server + "/" + mydatabase,user,password);
stmt=con.createStatement();
// prstmt=con.prepareStatement("insert into student values(?,?,?)");
System.out.println("connection ok");
}
catch(SQLException ex)
{
System.out.println("sql Exception");
}
}//end of constructor
public boolean saveData(String name,int rollno,int fee)throws SQLException
{
String sql;
sql="insert into student values('" + name + "'," + rollno + "," + rollno + ")";
stmt.execute(sql);
return true;
}//end of savedata
public void saveDataUsingPreparedStatement(String name,int rollno,int fee)throws SQLException{
prstmt=con.prepareStatement("insert into student values(?,?,?)");
prstmt.setString(1,name);
prstmt.setInt(2,rollno);
prstmt.setInt(3,fee);
prstmt.executeUpdate();
}//end of function
public void showData()throws SQLException{
prstmt=con.prepareStatement("select * from student");
rst=prstmt.executeQuery();
System.out.println("Name Rollno Fee");
System.out.print("**************************************");
while(rst.next())
{
System.out.println();
System.out.print(rst.getString(1) + " ");
System.out.print(rst.getInt(2) + " ");
System.out.print(rst.getInt(3) +" ");
System.out.println("****************************************");
}
}//end of function showData
}//end of class
public class DemoDatabase {
static String name;
static int rollno;
static int fee;
public static void main(String[] args) throws ClassNotFoundException,SQLException{
MyDatabaseConnection mydb = new MyDatabaseConnection();
Scanner sc=new Scanner(System.in);
int choice;
do {
System.out.println("1-------->Add");
System.out.println("2-------->Display");
System.out.println("3-------->Exit");
System.out.println("Enter your choice ?");
choice=sc.nextInt();
switch (choice) {
case 1:
//mydb.saveData("karan",601,3500);
getData();//calling function to getdata
mydb.saveDataUsingPreparedStatement(name, rollno, fee);
System.out.println("data saved to database");
System.out.println("***********************************************************************");
break;
case 2:
mydb.showData();
break;
case 3:
System.exit(1);
break;
}//end of switch
}while(true);
}//end of main
public static void getData(){
Scanner sc=new Scanner(System.in);
System.out.println("*****************************************************************");
System.out.println("Enter Name of Student : ");
name = sc.next();
System.out.println("Enter Rollno of Student :");
rollno = sc.nextInt();
System.out.println("Enter Fee of Student :");
fee = sc.nextInt();
}
}//end of main class
Note:
The above code is written to save the data into the database using PreparedStatement.The data is saved into the student table.