[学习笔记]

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

测试程序:

/*when you use single step to debug the program, you can find that Singleton only
is executed once.*/
import java.sql.*;
import javax.sql.*;

public class testSqlserverSingletonDataSource {

public static void main(String args[]) {
Connection con;

try {
con = SqlserverSingletonDataSource.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from authors");
while (rs.next()) {
System.out.print(rs.getString("au_id") + " ");
System.out.println(rs.getString("au_lname"));
}

}
catch (Exception e) {
}

System.out.println("the following is the second time ");

try {
con = SqlserverSingletonDataSource.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from authors");
while (rs.next()) {
System.out.print(rs.getString("au_id") + " ");
System.out.println(rs.getString("au_lname"));
}

}
catch (Exception e) {
}

}

}

文章转载自原文:https://blog.csdn.net/qq_43650923/article/details/100655993

最新文章

  1. Linux服务器安装笔记 汇总
  2. JS 劫持来源网站并做指定跳转
  3. Linux系统启动级别及顺序
  4. asp.net mvc在windows server 2003上部署
  5. JavaScript正则验证邮箱
  6. Linux性能实时监测工具 Netdata
  7. U10vim程序编辑器
  8. php 读取二进制文件
  9. 图片Ping
  10. W3CSchool闯关笔记(中级脚本算法)
  11. openshift 容器云从入门到崩溃之三《安装openshift》
  12. 6月17 练习ThinkPHP的增删改查
  13. Flume监听文件目录sink至hdfs配置
  14. Javascript-关于for in和forEach
  15. ERP渠道管理添加验证和查询(二十二)
  16. 浅谈EM算法的两个理解角度
  17. 获取作为 URL 部署清单的位置。
  18. JDK源码分析(10) ConcurrentLinkedQueue
  19. 开源工具 | 手游自动化框架GAutomator,新增iOS系统和UE4引擎支
  20. CentOS配置网易163 yum源

热门文章

  1. SpringMVC从Session域中获取值
  2. PHP用strtotime()函数比较两个时间的大小实例详解
  3. input标签type="file"上传文件的css样式
  4. 字节码(.class)文件的加载过程
  5. ftp配置 Laravel上传文件到ftp服务器
  6. Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 Tracepoint 是散落在内核源代码中的一些 hook,一旦使能,它们便可以在特定的代码被运行到时被触发,这一特性可以被各种 trace/debug 工具所使用。Perf 就是该特性的用户之一。
  7. YII2 composer update 报错解决一例-requires bower-asset/jquery 2.2
  8. Error-JavaScript-SCRIPT5007: 无法获取未定义或 null 引用的属性“style”
  9. Mac 打开、编辑 .bash_profile 文件
  10. osg gis编译日志