[学习笔记]

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

java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子的更多相关文章

  1. java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子

    [学习笔记] 6.Oracle DataSource and SingleTon: import oracle.jdbc.pool.OracleDataSource;import java.sql.C ...

  2. java当中JDBC当中请给出一个sql server的helloworld例子

    [学习笔记] 1.sql server的helloworld例子: import java.sql.*; public class JdbcHelloSqlServer {  public stati ...

  3. 请给出一个Scala RDD的HelloWorld例子

    [学习笔记]package comimport org.apache.spark.rdd.RDDimport org.apache.spark.SparkConfimport org.apache.s ...

  4. 已知一个函数rand7()能够生成1-7的随机数,请给出一个函数rand10(),该函数能够生成1-10的随机数。

    题目: 已知一个函数rand7()能够生成1-7的随机数,请给出一个函数,该函数能够生成1-10的随机数. 思路: 假如已知一个函数能够生成1-49的随机数,那么如何以此生成1-10的随机数呢? 解法 ...

  5. 请写出一个超链接,点击链接后可以向zhangsan@d-heaven.com发送电子邮件。

    请写出一个超链接,点击链接后可以向zhangsan@d-heaven.com发送电子邮件. <a href=”mailto: zhangsan@d-heaven.com”>发邮件</ ...

  6. java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子

    [学习笔记] 2.DataSource的单态模式(SingleTon)程序 咱们还接着上面的例子来说.1万个人要看书.千万确保要只建立一个图书馆.要是一不留神,建了两个或三个图书馆,那可就亏大发了.对 ...

  7. java中JDBC当中请给出一个DataSource的HelloWorld例子

    在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接.事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的.当然了,这也是没有办法的事儿.就像我们买 ...

  8. java当中JDBC当中请给出一个sql server的stored procedure例子

    3.sql server的stored procedure例子: import java.sql.*;public class StoredProc0 {public static void main ...

  9. java当中JDBC当中请给出一个sql server的dataSource的helloworld例子

     [学习笔记] 4. sql server的dataSource的helloworld: import java.sql.*;import javax.sql.*;import net.sourcef ...

随机推荐

  1. SpringCloud:入门介绍

    1.微服务简介 业界大牛马丁.福勒(Martin Fowler) 这样描述微服务: 论文网址:            https://martinfowler.com/articles/microse ...

  2. [代码审计]四个实例递进php反序列化漏洞理解【转载】

    原作者:大方子 原文链接:https://blog.csdn.net/nzjdsds/article/details/82703639 0x01 索引 最近在总结php序列化相关的知识,看了好多前辈师 ...

  3. Linux压缩和解压类指令

    一.gzip / gunzip  指令 gzip 用于压缩文件,gunzip 用于解压文件. 基本语法gizp  文件 (功能描述:只能将文件压缩为*.gz文件)gunzip  文件.gz (功能描述 ...

  4. web前端兼容性问题

    传送门:https://www.cnblogs.com/zhoudawei/p/7497544.html

  5. MITMProxy如何配置二次代理

    MITMProxy如何配置二次代理 0.2172018.09.05 11:13:15字数 232阅读 2609 前序: mitmproxy真的很强大,或许是大家都各自使用,或者没有相关的需求,导致我废 ...

  6. Object.keys()、Object.values()、Object.entries()的用法

    一.Object.keys(obj) 参数:要返回其枚举自身属性的对象 返回值:一个表示给定对象的所有可枚举属性的字符串数组 处理对象,返回可枚举的属性数组 let person = {name:&q ...

  7. yii2增删改查操作

    https://www.yiichina.com/tutorial/996 https://www.yiichina.com/tutorial/834 一.新增 使用model::save()操作进行 ...

  8. System.Runtime.Serialization.cs

    ylbtech-System.Runtime.Serialization.cs 允许对象控制其自己的序列化和反序列化过程. 1.返回顶部 1. #region 程序集 mscorlib, Versio ...

  9. Leetcode: Most Stones Removed with Same Row or Column

    On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at ...

  10. typeScript中的函数

    // 函数的定义 //es5定义函数的方法 /* //函数声明法 function run(){ return 'run'; } //匿名函数 var run2=function(){ return ...