JDBC工具类的使用

package com.jdbc.utils;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@SuppressWarnings("all")
public class JDBCUtils {

    private static  final String URL="jdbc:mysql://localhost:3306/mybase";
    private static final String USER="root";
    private static final String PASSWORD="123456";
    private static final String DRIVER="com.mysql.jdbc.Driver";

    //静态代码块加载驱动
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得数据库的连接
     * @return Connection
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL,USER,PASSWORD);
    }

    /**
     * 关闭操作
     * @param ca
     */
    public static void close(AutoCloseable... ca){

        for (AutoCloseable c:ca)
            if (c != null) {
                try {
                    c.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }


}

  

猜你喜欢

转载自www.cnblogs.com/qurui1997/p/10639783.html
今日推荐