首页 > 代码库 > 封装的DBHelper(JAVA)
封装的DBHelper(JAVA)
JDBC+Mysql
public class DBHelper { private static String driver ="com.mysql.jdbc.Driver"; private static String url ="jdbc:mysql://localhost:端口号(3306)/数据库名"; private static String username ="账号"; private static String password ="密码"; //静态, 连接 public static Connection ToConnection(){ Connection conn = null; try { Class.forName(driver); try { conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } //关闭连接 public static void CloseConnection(Connection conn){ if(conn !=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //关闭结果集 public static void CloseResultSet(ResultSet rs){ if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } rs=null; } } //关闭statement public static void ClosePreparedStatement(PreparedStatement psmt){ if(psmt!=null) { try { psmt.close(); psmt=null; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void CloseStatement(Statement stmt){ if(stmt!=null) { try { stmt.close(); stmt=null; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
封装的DBHelper(JAVA)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。