[学习笔记]
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) {
}
}
}
文章转载自原文:
如对本文有疑问, 点击进行留言回复!!
解决idea中出现“illegal character U+200B” 问题
荐 为什么加了@Transactional注解,事务没有回滚?
Attribute ‘sklearn.linear_model._logistic.LogisticRegression.multi_class‘ must be explicitly set to
Java/Python实现 LeetCode剑指Offer 14-I.剪绳子(动态规划)
网友评论