Java教程

连接数据库代码

本文主要是介绍连接数据库代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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);
        }
    }



}

 

这篇关于连接数据库代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!