java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子

5.SQLServer DataSource and SingleTon:


import net.sourceforge.jtds.jdbcx.*;
import java.sql.*;
import javax.sql.*;

public class SqlserverSingletonDataSource {
static private JtdsDataSource ds;
private Connection con;
private SqlserverSingletonDataSource() {

try {
ds = new JtdsDataSource();
ds.setServerName("localhost");
ds.setDatabaseName("pubs");
ds.setUser("sa");
ds.setPassword("");
}
catch (Exception e) {
}
}

public static Connection getConnection() throws Exception {
if (ds == null) {
new SqlserverSingletonDataSource();
}
Connection con =null;
try {
con = ds.getConnection();
} catch (SQLException ex) {
}

return con;
}
}

版权保护原文出处:http://www.mark-to-win.com/JavaBeginner/JavaBeginner10_web.html#SQLServerDataSourceandSingleTon

猜你喜欢

转载自blog.csdn.net/mark_to_win/article/details/89288855