- 自己写的对jdbc的封装,不过只做了简单的测试
2.所有操作都使用PreparedStatement完成,会为每个线程分别缓存每个Connection中相同sql的PrepareStatment对象
3.当连接被关闭时会自动重连(比如sql服务器重启了,重连不了就强退,不过可以改)
4.提供便捷ResultSet关闭方法
5.支持where in (xxx)
6.返回原生ResultSet对象
修改:2013年9月16日
1.增加 事物支持, 不过需要调用者自己管理事物
修改: 2013年9月17日 18:25:58
1.完善事物支持
2.更改部分方法
修改:2013年9月18日 17:21:21
1.为ResultSet增加自动关闭(通过动态代理)
2.改善重连机制
3.修改部分方法名,避免同名方法Object类型和String类型冲突,出现调用错误的现象
修改:2013年9月22日 15:51:31
增加第二个版本,因为第一个版本如果使用safe会为每个线程创建一个Connection,所以现在改成用连接池
//使用示列(第二版)
Object res = Qsql.cache("select count(*) from T_daily").queryOne();
print(res);
String beign = "2012-03-01";
String end = "2013-03-01";
List<Integer> pids = Arrays.asList(99,97);
Integer pid = null;
Integer spid = 202;
res = Qsql.cache("select count(*) from T_daily where dt > ?", beign)
.append(end!= null, " and dt < ?", end)
.in(pids!= null, " and pid in(?)", pids.toArray())
.append(pid!= null, " and pid=?", pid)
.append(spid!= null, " and spid=?", spid)
.queryOne();
print(res);
// 多in(?)示列
res = Qsql.cache("select count(*) from T_daily where dt > in(?) and pid in(?)")
.in("2013-12-03", "2013-12-05").in(21,23);
print(res);
res = Qsql.cache("select count(*) from T_daily where")
.in(" dt > in(?) and pid in(?)", "2013-12-03", "2013-12-05").in(21,23);
print(res);
// autoClose示列
ResultSet rst = Qsql.cache("select count(*) from T_daily where")
.in(" pid in(?)", 99, 97) // 注意in和append的前面要+空格, "in(?)"必须没有空格间隙
.append(" and dt > ? group by pid", "2013-07-01")
.autoClose() // rst的方法抛出异常或while next循环退出后自动关闭ResultSet
.query();
while(rst.next()) {
print(rst.getObject(1));
}
print("RST: " + rst.isClosed());
rst = Qsql.cache("select count(*) from T_daily where")
.in(" (pid in(?)", 99, 97) // 注意in和append的前面要+空格, "in(?)"必须没有空格间隙
.append(" and dt > ?) group by pid,spid limit ?", "2013-07-01", 5)
.autoClose() // rst的方法抛出异常或while next循环退出后自动关闭ResultSet
.query();
while(rst.next()) {
print(rst.getObject(1));
}
print("RST: " + rst.isClosed());
// 事务
int carId = 1;
int userId = 1;
int productId = 1;
int productCount = 3;
Qsql insertCar = Qsql.cache("insert into car(carId,productId,productCount,state) values (?,?,?,?)", carId, productId, productCount, 0);
Qsql updateCar = Qsql.cache("set state=?", 1).append(" from car where productId=? and carId = ?", productId, carId);
Qsql updateUserInfo = Qsql.cache("set boughtProducts=boughtProducts+? from user,car where car.userId = user.userId and car.carId = ?", productCount, carId);
insertCar.update();
Qsql.beginTransaction(Connection.TRANSACTION_READ_COMMITTED);
try {
updateCar.update();
updateUserInfo.update();
pay(userId, carId, productCount);
Qsql.commit();
} catch (Exception e) {
e.printStackTrace();
Qsql.rollback();
}
print(res);
String beign = "2012-03-01";
String end = "2013-03-01";
List<Integer> pids = Arrays.asList(99,97);
Integer pid = null;
Integer spid = 202;
res = Qsql.cache("select count(*) from T_daily where dt > ?", beign)
.append(end!= null, " and dt < ?", end)
.in(pids!= null, " and pid in(?)", pids.toArray())
.append(pid!= null, " and pid=?", pid)
.append(spid!= null, " and spid=?", spid)
.queryOne();
print(res);
// 多in(?)示列
res = Qsql.cache("select count(*) from T_daily where dt > in(?) and pid in(?)")
.in("2013-12-03", "2013-12-05").in(21,23);
print(res);
res = Qsql.cache("select count(*) from T_daily where")
.in(" dt > in(?) and pid in(?)", "2013-12-03", "2013-12-05").in(21,23);
print(res);
// autoClose示列
ResultSet rst = Qsql.cache("select count(*) from T_daily where")
.in(" pid in(?)", 99, 97) // 注意in和append的前面要+空格, "in(?)"必须没有空格间隙
.append(" and dt > ? group by pid", "2013-07-01")
.autoClose() // rst的方法抛出异常或while next循环退出后自动关闭ResultSet
.query();
while(rst.next()) {
print(rst.getObject(1));
}
print("RST: " + rst.isClosed());
rst = Qsql.cache("select count(*) from T_daily where")
.in(" (pid in(?)", 99, 97) // 注意in和append的前面要+空格, "in(?)"必须没有空格间隙
.append(" and dt > ?) group by pid,spid limit ?", "2013-07-01", 5)
.autoClose() // rst的方法抛出异常或while next循环退出后自动关闭ResultSet
.query();
while(rst.next()) {
print(rst.getObject(1));
}
print("RST: " + rst.isClosed());
// 事务
int carId = 1;
int userId = 1;
int productId = 1;
int productCount = 3;
Qsql insertCar = Qsql.cache("insert into car(carId,productId,productCount,state) values (?,?,?,?)", carId, productId, productCount, 0);
Qsql updateCar = Qsql.cache("set state=?", 1).append(" from car where productId=? and carId = ?", productId, carId);
Qsql updateUserInfo = Qsql.cache("set boughtProducts=boughtProducts+? from user,car where car.userId = user.userId and car.carId = ?", productCount, carId);
insertCar.update();
Qsql.beginTransaction(Connection.TRANSACTION_READ_COMMITTED);
try {
updateCar.update();
updateUserInfo.update();
pay(userId, carId, productCount);
Qsql.commit();
} catch (Exception e) {
e.printStackTrace();
Qsql.rollback();
}