import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
class Main {
public static void main(String args[]) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
DataSource dataSource = (DataSource) ac.getBean("dataSource");
// DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String firstCustomersLastName = (String) jdbcTemplate.query(
"select last_name from t_customer where id=?",
new Object[]{1L}, new int[]{Types.INTEGER},
new FirstColumnStringExtractor());
System.out.println(firstCustomersLastName);
}}
class FirstColumnStringExtractor implements ResultSetExtractor {
public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException {
if (resultSet.next()) {
return resultSet.getString(1);
}
return null;
}
}
|