JDBC Statements
To Send a request the database and retrieve results from the database we need to create statements.
Type of Statements : Statement PreparedStatement
CallableStatement
Confidential
Statement
This is an interface provided by JDBC- API to process the simple JDBC queries to the Database.
Methods of Statement interface : executeUpdate() executeQuery()
setMaxRows()
getMaxRows(); close()
Confidential
Practical Implmentation of JDBC
import [Link].*; public class Main { public static void main(String[] args) { try { [Link]("[Link]"); Connection con=[Link]("jdbc:odbc:dsn1","sa",""); Statement stat=[Link](); ResultSet rs=[Link]("select * from login");
} }
while([Link]())
{ [Link]("User Name "+[Link](1)); [Link]("Pin Number "+[Link](2));
catch(Exception e)
} }
{
[Link](e); }
Confidential
PreparedStatement
This interface is provide by the JDBC-API to process parameterized Query to the database.
Methods of PreparedStatement : executeQuery() executeUpdate()
setString()
setInt() setDouble() close();
Confidential
Practical Implmentation of JDBC
import [Link].*; public class Main {
public static void main(String[] args)
{ try { [Link]("[Link]"); Connection con=[Link]("jdbc:odbc:dsn1","sa",""); PreparedStatement stat=[Link]("select * from login where userName=?"); [Link](1,"Rajan");
ResultSet rs=[Link]();
while([Link]()) { [Link]("User Name "+[Link](1)); [Link]("Pin Number "+[Link](2)); } catch(Exception e) {
} } }
[Link](e);
Confidential
CallableSatement
This is an interface provided by JDBC- API to process Stored Procedure calls.
Methods of CallableStatement interface : setString() setInt()
setDouble()
getString() getInt() getDouble() close()
Confidential
Practical Implementation of JDBC
import [Link].*; public class Main { public static void main(String[] args) { try
{
[Link]("[Link]"); Connection con=[Link]("jdbc:odbc:dsn1","sa",""); String str="{call MyProcedure(?,?,?,?)}"; CallableStatement cs=[Link](str); [Link](1,"1001"); [Link](2,[Link]); [Link](3,[Link]); [Link](4,[Link]); [Link](); String fname=[Link](2); String lname=[Link](3); String city=[Link](4); [Link](fname); } catch(Exception e) {
} } }
[Link](e);
Confidential