zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Python标准异常topic
    文件打开的模式和文件对象方法
    python中常用的一些字符串
    zabbix3.2.0beta2 监控模版
    人工智能 --test
    Jenkins 2.7.3 LTS 发布
    Python中的socket 模块
    hibenater返回map
    去掉JavaScript Validator
    properties工具类
  • 原文地址:https://www.cnblogs.com/haima1949/p/11622374.html
Copyright © 2011-2022 走看看