Wednesday, 11 December 2013

jdbc program using PreparedStatement

//jdbc program using PreparedStatement
import java.sql.*;

public class PStatEx1 {

public static void main(String s[]) throws Exception {

Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection ("jdbc:mysql://localhost:3306/test","root","admin");

String query="insert into mytable values (?,?,?)";

//Step1: Get PreparedStatement
PreparedStatement ps=con.prepareStatement (query);

//Step2: set parameters
                ps.setInt(1,26);
                ps.setString(2,"Rajendra");

ps.setString(3,"hyd");

//Step3: execute the query
int i=ps.executeUpdate();

System.out.println("record inserted count:"+i);

//To execute the query once again
ps.setInt(1,39);
ps.setString(2,"pavan");

ps.setString(3,"Blore");

i=ps.executeUpdate();
System.out.println("query executed for the second time count: "+i);
con.close();
}//main
}//class

No comments:

Post a Comment