JDBC通过Single模式连接Mysql

    技术2022-06-09  35

    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); } }  


    最新回复(0)