databaseidgenerator.java

来自「这是一本描述JDBC数据库的书籍」· Java 代码 · 共 70 行

JAVA
70
字号
/* * DatabaseIDGenerator.java * * Created on June 10, 2005, 10:58 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package ch02.oid;import java.sql.*;import javax.sql.*;import common.*;/** * * @author kevin */public class DatabaseIDGenerator {    public final static long ID_PERSON = 2;    public final static long ID_STUDENT = 21;    public final static long ID_EMPLOYEE = 22;        public synchronized static long nextPersonID(){        return nextID(ID_PERSON);    }        public synchronized static long nextStudentID(){        return nextID(ID_STUDENT);    }        public synchronized static long nextEmployeeID(){        return nextID(ID_EMPLOYEE);    }        private static long nextID(long tableID){        Connection conn = null;        Statement  stmt = null;        ResultSet  rs = null;        long result = 0;        try{            ConnectionFactory factory = ConnectionFactory.getConnectionFactory();            conn = factory.getConnection();            stmt = conn.createStatement();            String sql = "select nextval from oid_tbl where type=" + tableID;            rs = stmt.executeQuery(sql);            if(rs.next()){                result = rs.getLong("nextval");            }            String updateSql = "update oid_tbl set nextval = nextval where type=" + tableID;            stmt.executeUpdate(updateSql);        }catch(Exception e){            e.printStackTrace();        }finally{            DBUtils.close(rs, stmt, conn);        }        return result;    }    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?