链接数据库

package com.qq.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 连接数据库工具类,使用单例模式
 * @author user
 *
 */
public class DBUtil {

    private static String url = "jdbc:mysql://localhost:3306/数据库表名称";
    private static String user = "root";
    private static String pwd = "root";
    private static Connection conn ;

    private DBUtil(){

    }

    static{
        //1.动态加载驱动程序
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, pwd);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得数据库连接的方法
     */
    public static Connection getConn(){
        return conn;
    }

}

你可能感兴趣的:(java,连接数据库)