依次关闭使用对象及连接
ResultSet---Statement--Connection
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 测试ResultSet结果集的基本用法
* @author Administrator
*/
public class Demo004 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/testjdbc", "root", "");
String sql = "select id,username,pwd from t_user where id>?";//?占位符
ps = conn.prepareStatement(sql);
ps.setObject(1, 3);//把id大于3的记录都取出来
rs = ps.executeQuery();
while (rs.next()) {
System.out
.println(rs.getInt("id") + "--"
+ rs.getString("username") + "--"
+ rs.getString("pwd"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}