//關閉打開的全部鏈接mysql
public class Demo4 { 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:3306/test","root","dyl123"); String sql="select * from t_user where id>?"; ps=conn.prepareStatement(sql); ps.setInt(1, 2); rs=ps.executeQuery(); //返回結果集 //遍歷結果集 //rs.next(),指向結果集中的第一條,而後每次指向下一條 //rs.getInt(num),得到結果集中第num列的int類型的內容,rs.getString(num)得到結果集中第num列的String類型的內容 while(rs.next()) { System.out.println(rs.getInt(1)+"--"+rs.getString(2)+"--"+rs.getString(3)); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(null!=rs) { rs.close(); } }catch (SQLException e) { e.printStackTrace(); } try { if(null!=ps) { ps.close(); } }catch (SQLException e) { e.printStackTrace(); } try{ if(null!=conn) { conn.close(); } }catch (SQLException e) { e.printStackTrace(); } } } }