package org.local.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public final class JdbcUtilSingle {
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "";
private void JdbcUtilSingle(){}//不允许直接实例化
public static JdbcUtilSingle instance = null;
public static JdbcUtilSingle getInstance(){//单例模式
if( instance == null ){
synchronized(JdbcUtilSingle.class){//防止多线程引起产生多个实例
if( instance == null ){
instance = new JdbcUtilSingle();//只允许实例化对象一次
}
}
}
return instance;
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}
}