[学习笔记]

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. phpMyadmin各个版本漏洞【转载】

    原作者:热爱网络安全的小菜狗 原文链接:phpMyadmin各版本漏洞 0x01 PREGREPLACEEVAL漏洞 影响版本:3.5.x < 3.5.8.1 and 4.0.0 < 4. ...

  2. Fluent批处理之--windows下多个任务的计算 【转载】

    转载自http://jingcao830828.blog.163.com/blog/static/10320833620103633624506/ 1.同维多任务的连续计算 对于工程应用来说,计算精度 ...

  3. 第07组 Alpha冲刺(3/6)

    队名:摇光 队长:杨明哲 组长博客:求戳 作业博客:求再戳 队长:杨明哲 过去两天完成了哪些任务 文字/口头描述:对后端功能进一步完善. 展示GitHub当日代码/文档签入记录:(组内共用,已询问过助 ...

  4. Python Django使用HttpResponse返回图片并显示

    views.py def read_img(request): """ : 读取图片 :param request: :return: """ ...

  5. java和c# md5加密

    MD5加密的方式有很多,加盐的方式更多,最近项目需要java和c#加密结果一致,形成方法如下: 1.c#加密方法/// <summary> /// MD5 加密字符串 /// </s ...

  6. Visual Studio 2019更新到16.2.2

    Visual Studio 2019更新到16.2.2 此次更新的变化有以下几点: (1)修复了Visual Studio在关闭时的停止响应问题.(2)修复CVE-2019-1211权限提升漏洞.(3 ...

  7. CentOS 7 yum Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile

    yum install nginx发生的错误 yum install nginx Loaded plugins: fastestmirror, ovl Loading mirror speeds fr ...

  8. IFC构件位置数据与revit模型中对应构件位置数据对比

    IFC构件位置数据与revit模型中对应构件位置数据对比

  9. 转 Java连接Oracle数据库的简单示例

    https://www.cnblogs.com/joyny/p/11176643.html https://community.oracle.com/thread/4096458 import jav ...

  10. 123457123456#0#----com.DoraGame.AiMiYu20--前拼后广--caimi-doraX

    com.DoraGame.AiMiYu20--前拼后广--caimi-doraX