public class MicroJDBCExampleStatementPrepared {
String jdbcDriver = "oracle.jdbc.driver.OracleDriver";
String jdbcURL = "jdbc:oracle:thin:@localhost:1521:TEST";
String jdbcUserName = "myUserName";
String jdbcPassword = "myPassword";
public MicroJDBCExampleStatementPrepared(){
}
class EmployeeSelect extends MicroJDBCRecordImpl{
int id;
String name;
String lastName;
public EmployeeSelect(){
setSqlStatementType(2); // prepared
statement
}
public String getSelectSql() {
return "SELECT
id, name, lastname FROM Employee WHERE id = ?";
}
public void prepareSelect(PreparedStatement ps) throws SQLException
{
ps.setInt(1,id);
}
public void select(ResultSet rs) throws SQLException {
name = rs.getString(2);
lastName = rs.getString(3);
}
}
public void exampleSelect(int employeeId) throws MicroJDBCException{
MicroJDBCDBase
mdb = new MicroJDBCDBase();
mdb.setConnection( jdbcDriver, jdbcURL, jdbcUserName, jdbcPassword);
EmployeeSelect
employee = new EmployeeSelect();
employee.id = employeeId;
mdb.selectRow(employee);
log("EmployeeId:" + employee.id +
", EmployeeName:" + employee.name +
", EmployeeLastName:" + employee.lastName);
}
class EmployeeUpdate extends MicroJDBCRecordImpl{
int id;
String name;
String lastName;
public EmployeeUpdate(){
setSqlStatementType(2); // prepared
statement
}
public void prepareUpdate(PreparedStatement ps) throws SQLException
{
ps.setInt(3,id);
ps.setString(1,name);
ps.setString(2,lastName);
}
public String getUpdateSql() {
return "UPDATE
Employee set name=?, lastname=? WHERE id = ?";
}
}
public void exampleUpdate(int employeeId) throws MicroJDBCException{
MicroJDBCDBase
mdb = new MicroJDBCDBase();
mdb.setConnection( jdbcDriver, jdbcURL, jdbcUserName, jdbcPassword);
EmployeeUpdate
employee = new EmployeeUpdate();
employee.id = employeeId;
employee.name = "John";
employee.lastName = "Doe";
mdb.update(employee);
}
public static void main(String[] args) {
MicroJDBCExampleStatementPrepared
tinyJDBCExample = new MicroJDBCExampleStatementPrepared();
try{
tinyJDBCExample.exampleSelect(12);
}catch (MicroJDBCException
e){
e.printStackTrace();
}
}
private void log(String
line){
System.out.println(line);
}
}