-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.java
64 lines (61 loc) · 1.44 KB
/
Counter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.gootrip.util;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
/**
* <p>Title: 计数器类</p>
* <p>Description: 获取最大序列数</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author advance.wu
* @version 1.0
*
* sql:
*
* CREATE TABLE `tblserial` (
`tablename` varchar(100) NOT NULL default '',
`serialno` int(11) NOT NULL default '0',
PRIMARY KEY (`tablename`)
)
*/
public class Counter {
private static Counter counter = new Counter();
private Counter() {
}
public static Counter getInstance(){
return counter;
}
/**
* 获取最大序列号
* @param strTable 表名
* @param con 数据库链接
* @return
*/
public synchronized long nextSerial(String strTable,Connection con){
String strSQL = null;
long serialno = 0;
Statement stmt = null;
try {
strSQL = "select serialno from tblserial where tablename='" + strTable + "'";
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
if (rs.next())
serialno = rs.getLong(1);
strSQL = "update tblserial set serialno = serialno + 1 where tablename='" + strTable + "'";
stmt.execute(strSQL);
rs.close();
rs = null;
serialno ++;
}
catch (Exception ex) {
}finally{
if (stmt != null)
try {
stmt.close();
stmt = null;
}
catch (Exception ex) {}
}
return serialno;
}
}