本文主要是介绍懒汉式单利同步链接jdbc数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.fh.util.test;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;/** * 类名称:SingletonJDBCConnection 测试链接数据库* 创建人:toshiba * 创建时间:2017年9月2日16:11:12* @version*/
public class SingletonJDBCConnection {private static SingletonJDBCConnection instance;private SingletonJDBCConnection() {super();}public static SingletonJDBCConnection getInstance(){if(null==instance){synchronized (SingletonJDBCConnection.class) {if(null==instance){instance = new SingletonJDBCConnection();}return instance;}}return instance;}private static final String URL="jdbc:mysql://127.0.0.1:3306/*****?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8";private static final String USERNAME="****";private static final String PASSWORD="****";private Connection conn=null;protected Connection getConnection(){try {DriverManager.registerDriver(new com.mysql.jdbc.Driver());conn =DriverManager.getConnection(URL, USERNAME, PASSWORD);} catch (SQLException e) {e.printStackTrace();} return conn;}
}
这篇关于懒汉式单利同步链接jdbc数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!