日期:2014-05-16 浏览次数:20648 次
create table keytable( keyname varchar2(20) primary key, keyvalue number(10) not null);
package singleton.keySequence;
public class KeyInfo {
private long minKey;
private long maxKey;
private int poolSize;
private long nextKey;
private String keyName;
/**
* @param poolSize 键的缓冲长度
* @param keyName 键的名字
*/
public KeyInfo(int poolSize,String keyName){
this.poolSize=poolSize;
this.keyName=keyName;
queryDB();
}
/**
* @return the value of nextKey 用于获得下一个键的方法
* 如果返回的键值大于键的上限,查询数据库重新进行对键的最大值,最小值和下一个返回键值的设定
*/
public long getNextKey(){
if(nextKey>maxKey){
queryDB();
}
return nextKey++;
}
/**
* 查询数据库重新进行对键的最大值,最小值和下一个返回键值的设定
*/
private void queryDB(){
String updateSql="UPDATE KEYTABLE SET KEYVALUE=KEYVALUE+"
+poolSize+" WHERE KEYNAME='"+keyName+"'";
String querySql="select KEYVALUE FROM KEYTABLE WHERE KEYNAME='"+keyName+"'";
JDBCTemplate.update(updateSql);
// System.out.println(updateSql);
// System.out.println(querySql);
long valueFromDB=(Integer)JDBCTemplate.query(querySql); maxKey=valueFromDB;
minKey=maxKey-poolSize+1;
nextKey=minKey;
}
}package singleton.keySequence;
import java.util.HashMap;
import java.util.Map;
public class KeyGenerator {
private Map<String,KeyInfo> keyMap=new HashMap<String,KeyInfo>(10);
private static final int POOL_SIZE=30;
private static KeyGenerator keygen=new KeyGenerator();
private KeyGenerator() {
}
public static KeyGenerator getInstance(){
return keygen;
}
public long getNextKey(String keyName){
if(keyMap.containsKey(keyName)){
return keyMap.get(keyName).getNextKey();
}else{
KeyInfo keyInfo=new KeyInfo(POOL_SIZE,keyName);
keyMap.put(keyName, keyInfo);
return keyInfo.getNextKey();
}
}
}package common.db;
import java.sql.*;
import java.util.*;
import java.io.*;
public class JDBCConnectionFactory
{
static String url;
static String user;
static String password;
//从文件中加载数据库的连接信息
//目的:降低耦合度,即更换数据库时无需更改源代码。
static
{
try
{
FileInputStream fis=new FileInputStream("common/db/dbtext");
Properties pro=new Properties();
pro.load(fis);
url=pro.getProperty("url");
user=pro.getProperty("username");
password=pro.getProperty("password");
fis.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
//功能一:
public static Connection getConnection()
{
Connection conn=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
//功能三:
public static void close(Connection conn,ResultSet rs,Statement stmt)
{
try
{
if(conn!=null)
conn.close();
if(stmt!=null)
stmt.close();
if(rs!=null)
rs.close();
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}package common.db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTemplate {
private static Connection conn=null;
private static Statement stmt=null;
private static ResultSet rs=null;
public static void update(String updateSql) throws Exception{
conn=JDBCConnectionFactory.getConnection();
stmt=conn.createStatement();
stmt.executeUpdate(updateSql);
JDBCConne