DataSource ds = DataSourceUtils.getDataSourceFromJndi("MyDS");
JdbcTemplate jdbc = new JdbcTemplate(ds);
jdbc.execute("drop table TEMP");
jdbc.update("update EMPLOYEE set FIRSTNME=? where LASTNAME=?",
new String[] {"JOE", "LEE"});
int maxAge = jdbc.queryForInt("select max(AGE) from EMPLOYEE");
String name = (String)jdbc.queryForObject(
"select FIRSTNME from EMPLOYEE where LASTNAME=''LEE''", String.class);
List employees = jdbc.queryForList(
"select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE");
final List employees = new LinkedList();
jdbc.query("select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE",
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
Employee e = new Employee();
e.setEmpNo(rs.getString(1));
e.setFirstName(rs.getString(2));
e.setLastName(rs.getString(3));
employees.add(e);
}
});
jdbc.call(new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection conn)
throws SQLException {
return conn.prepareCall("my query");
}
}, params);
BatchPreparedStatementSetter setter =
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i)
throws SQLException {
...
}
public int getBatchSize() {
return ...;
}
};
jdbc.batchUpdate("update ...", setter);
class EmployeeQuery extends MappingSqlQuery {
public EmployeeQuery(DataSource ds) {
super(ds, "select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE where EMPNO = ?");
declareParameter(new SqlParameter(Types.CHAR));
compile();
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
Employee e = new Employee();
e.setEmpNo(rs.getString("EMPNO"));
e.setFirstName(rs.getString("FIRSTNME"));
e.setLastName(rs.getString("LASTNAME"));
return e;
}
public Employee findEmployee(String id) {
return (Employee) findObject(id);
}
}
SqlFunction sf = new SqlFunction(dataSource,
"select count(*) from mytable");
sf.compile();
int rows = sf.run();
try {
// do work
} catch (OptimisticLockingFailureException ex) {
// I''m interested in this
}
补充:Jsp教程,Java基础