public class DBUtil { Connection conn = null; Statement stmt = null; PreparedStatement ps = null; ResultSet rs = null; String url = null; String user = null; String password = null; String sql = null; int number; public void openDb() { try { Class.forName("com.mysql.jdbc.Driver"); // } catch (ClassNotFoundException e) { System.out.println(""); e.printStackTrace();// } try { url = "jdbc:mysql://localhost:3306/db"; user = "root"; password = "root"; conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { System.out.println("驱动加载异常"); e.printStackTrace(); } } public ResultSet query(String sql){ try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); } catch (SQLException e) { System.out.println("sql查询语句异常"); e.printStackTrace(); } return rs; } public Boolean insert(String sql){ boolean boo=false; try { stmt = conn.createStatement(); stmt.executeUpdate(sql);// 执 boo=true; } catch (SQLException e) { System.out.println("插入语句异常"); e.printStackTrace(); } return boo; }
public void closeDb() { try { if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (Exception e) { System.out.println("sql连接关闭异常"); e.printStackTrace(); } }}