日期:2014-05-16 浏览次数:20415 次
package com.singleton; import java.sql.Connection; import java.sql.DriverManager; public class DBConnection { private static DBConnection dbc; private String url="jdbc:sqlserver://10.10.0.98:1433;databaseName=mydb"; private String user="username"; private String password="password"; private Connection conn; //私有构造器,只允许内部实用,不允许在外面调用创建类实例 private DBConnection(){ } public static DBConnection getInstance(){ if (dbc == null) { dbc = new DBConnection(); } return dbc; } public Connection getConnection(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); if (conn == null){ conn= DriverManager.getConnection(url,user,password); } } catch (Exception e){ e.printStackTrace(); } return conn; } }