A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄廷 中级黑马   /  2019-4-17 21:33  /  723 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


package com.itheima.utils;import java.sql.*;/**12 / 21
* 访问数据库的工具类*/
public class JdbcUtils {
//可以把几个字符串定义成常量:用户名,密码,URL,驱动类
private static final String USER = "root";
private static final String PWD = "root";
private static final String URL = "jdbc:mysql://localhost:3306/day24";
private static final String DRIVER= "com.mysql.jdbc.Driver";
/** * 注册驱动 */
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} }
/** * 得到数据库的连接 */
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL,USER,PWD);
}
/** * 关闭所有打开的资源 */
public static void close(Connection conn, Statement stmt) {
if (stmt!=null) {
try { stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} } if (conn!=null) {
try { conn.close();
} catch (SQLException e) {
e.printStackTrace(); } } }
/** * 关闭所有打开的资源 */
public static void close(Connection conn, Statement stmt, ResultSet rs) {
if (rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } close(conn, stmt); }}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马