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;
}
}
测试程序:
/*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
最新文章
- Linux服务器安装笔记 汇总
- JS 劫持来源网站并做指定跳转
- Linux系统启动级别及顺序
- asp.net mvc在windows server 2003上部署
- JavaScript正则验证邮箱
- Linux性能实时监测工具 Netdata
- U10vim程序编辑器
- php 读取二进制文件
- 图片Ping
- W3CSchool闯关笔记(中级脚本算法)
- openshift 容器云从入门到崩溃之三《安装openshift》
- 6月17 练习ThinkPHP的增删改查
- Flume监听文件目录sink至hdfs配置
- Javascript-关于for in和forEach
- ERP渠道管理添加验证和查询(二十二)
- 浅谈EM算法的两个理解角度
- 获取作为 URL 部署清单的位置。
- JDK源码分析(10) ConcurrentLinkedQueue
- 开源工具 | 手游自动化框架GAutomator,新增iOS系统和UE4引擎支
- CentOS配置网易163 yum源
热门文章
- SpringMVC从Session域中获取值
- PHP用strtotime()函数比较两个时间的大小实例详解
- input标签type=";file";上传文件的css样式
- 字节码(.class)文件的加载过程
- ftp配置 Laravel上传文件到ftp服务器
- Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 Tracepoint 是散落在内核源代码中的一些 hook,一旦使能,它们便可以在特定的代码被运行到时被触发,这一特性可以被各种 trace/debug 工具所使用。Perf 就是该特性的用户之一。
- YII2 composer update 报错解决一例-requires bower-asset/jquery 2.2
- Error-JavaScript-SCRIPT5007: 无法获取未定义或 null 引用的属性“style”
- Mac 打开、编辑 .bash_profile 文件
- osg gis编译日志