📄 hl.java
字号:
package com.briup;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
public class HL {
// 保存当前的high值
private static int high;
// 定义low值
private static int user_id = 1;
private static int order_id = 1;
//定义一个map来存储缓存里面highlow值的使用情况,
//("user",oid),("order",oid)
private static Map<String, Integer> map =
new HashMap<String, Integer>();
/**为user提供oid*/
synchronized public static int nextVal_user(Connection conn){
int id = 0;
Integer userId = map.get("user");
if(userId == null){
high = base(conn,"user");
}
id = high + user_id;
map.put("user", id);
high = map.get("user");
//user_id++;
return id;
}
synchronized public static int nextval_Order(Connection conn){
return 0;
}
/**
* 读取并即时更新高值
* */
synchronized public static int base(Connection conn,String user){
String sql =
"select nextval from hl_tbl for update";
Statement stmt = null;
ResultSet rs = null;
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
high = rs.getInt(1);
}
Integer oid_user = map.get("user");
String sqlUpdate = "";
if(oid_user != null)
sqlUpdate =
"update hl_tbl set nextval = "+ oid_user;
else
sqlUpdate = "update hl_tbl set nextval = "+(high + 1);
stmt.executeUpdate(sqlUpdate);
}catch(Exception e){
e.printStackTrace();
}finally{
DBUtil.close(rs, stmt, null);
}
return high;
}
public static void main(String[] ags){
Connection conn =
JDBCConnectionFactory.getConnection();
for(int i=0;i<20;i++){
System.out.print(nextVal_user(conn)+"\t");
}
base(conn,"user");
DBUtil.close(conn);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -