基本过程
1.获取连接 (Connection)
2.创建语句 (PrepareStatement)
3.绑定参数(setxxx)
4.执行语句(execute)
5.清理资源(close)
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "system.spring.xml" });
DataSource dataSource = (DataSource) context.getBean("datasource");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement("select id,username,fullname from t_spitter where id = ?");
ps.setLong(1, 1);
rs = ps.executeQuery();
if (rs.next()) {
System.out.println(rs.getLong(1));
System.out.println(rs.getString("username"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}