连接数据库代码

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

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

public class JDutil {
//    声明一个连接池对象
    private static DruidDataSource druidDataSource;
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
    static {
        Properties properties = new Properties();
        InputStream inputStream = JDutil.class.getResourceAsStream("/datebase.properties");
        try {
            properties.load(inputStream);
//            创建连接池
            try {
                druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //1.获取连接
    public static Connection getConnection()
    {

        try {
            return druidDataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
//        String url = "jdbc:mysql://localhost:3306/shujuku?&useUnicode=true&serverTimezone=UTC";
//        String user = "root";
//        String password = "123456";
//        Connection connection = threadLocal.get();
//        try {
////            String url = "jdbc:mysql://localhost:3306/shujuku?&useUnicode=true&serverTimezone=UTC";
////            String user = "root";
////            String password = "123456";
//            if(threadLocal.get() == null) {
//                connection = DriverManager.getConnection(url, user, password);
//                threadLocal.set(connection);
//            }
////            System.out.println(connection);
//        }
//        catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return connection;
    }
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet)
    {
        try {
            if(resultSet != null)
                resultSet.close();
            if(statement != null)
                statement.close();
            if(connection != null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void begin()
    {
        Connection connection = getConnection();

        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void commit()
    {
        Connection connection = getConnection();
        try {
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,null,null);
        }
    }
    public static void rollback()
    {
        Connection connection = null;
        try {
            connection = getConnection();
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,null,null);
        }
    }



}

 

posted @ 2021-11-04 22:29  软工小蜗牛  阅读(593)  评论(0编辑  收藏  举报