工具类_初

“获得数据库连接”操作,将在以后的增删改查所有功能中都存在,可以封装工具类JDBCUtils。
提供获取连接对象的方法,从而达到代码的重复利用。

未加配置文件

package demo;

import java.sql.*;

public class JDBCUtils {
static String url = “jdbc:mysql://localhost:3306/day04”;
static String user = “root”;
static String password = “root”;
//静态代码块,随类加载

static {
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
//获取连接对象
public static Connection conn() throws SQLException {
    Connection connection = DriverManager.getConnection(url,user,password);
    return connection;
}


//给与外部调用
public static void Close(Connection connection, Statement statement, ResultSet resultSet){
    connectionClose(connection);
    statementClose(statement);
    resultClose(resultSet);
}
//私有方法让关闭流
private static void statementClose(Statement statement){
    if (statement != null){
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
private static void connectionClose(Connection connection){
    if (connection != null){
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
private static void resultClose(ResultSet resultSet){
    if (resultSet != null){
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

加配置文件

package mysql;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static {
try {

        Properties properties = new Properties();
        //导入输入流
        InputStream is = new FileInputStream("G:\\javaEE\\develop_mysql\\mysqlCode\\src\\jdbc.properties");
       // InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        //加载配置文件
        properties.load(is);
        //获取配置文件数据
        driverClass = properties.getProperty("driverClass");
      //  System.out.println(driverClass);
        url = properties.getProperty("url");
        name= properties.getProperty("name");
        password= properties.getProperty("password");

        Class.forName(driverClass);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static Connection getConn() throws SQLException {
    //注册驱动
    Connection conn = DriverManager.getConnection(url, name, password);

/* try {
conn = null;
DriverManager.registerDriver(new com.mysql.jdbc.Driver());

        System.out.println(name);
        连接(协议+访问数据库 用户名 密码)*/

        //和数据库打交道,必须用此
  /*  } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }*/
    return conn;
}

/*
* 释放资源
* */
public static void release(Connection conn, Statement st, ResultSet rs){
    closeSt(st);
    closeConn(conn);
    closeRs(rs);
}

private static void closeRs(ResultSet rs){
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
private static void closeSt(Statement st){
    if (st !=  null) {
        try {
            st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
private static void closeConn(Connection conn){
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值