Prepared Statement in Java With Example

Prepared Statement in Java With Example. This Java Prepared Statement example describes the basic operations performed on the JDBC Prepared Statement database connectivity code.

Prepared Statement in Java With Example. This Java Prepared Statement example describes the basic operations performed on the JDBC Prepared Statement database connectivity code.In this example you will learn about how to write java code for Prepared Statement with oracle database.

Program:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PreparedStmtDemo {
public static void main(String args[]){
Connection con=null;
PreparedStatement pst = null;
System.out.println(“conn”);
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
con=DriverManager.getConnection(
“jdbc:oracle:thin:@localhost:1521:ORCL”,”scott”,”tiger”);
pst = con.prepareStatement(“select * from eshusoft where username=?”);
pst.setString(1, “myuser”);
ResultSet rs = pst.executeQuery();
if(rs.next()) {
String userName= rs.getString(1);
String password = rs.getString(2);
System.out.println(userName+”: “+password);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Result:

myuser : mypwd2