import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
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.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
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);
List<Long> ids;
ids = (List<Long>) jdbcTemplate.execute("select id from t_x", new MyPreparedStatementCallback());
System.out.println(ids);
}
}
class MyPreparedStatementCallback implements PreparedStatementCallback {
public Object doInPreparedStatement(PreparedStatement preparedStatement) throws SQLException, DataAccessException {
ResultSet rs = preparedStatement.executeQuery();
List<Long> ids = new LinkedList<Long>();
while (rs.next()) {
ids.add(rs.getLong(1));
}
rs.close();
return ids;
}
}
|