incrementidgenerator.java
来自「软件设计课做的一个类似Hibernate的O/R Mapping的框架」· Java 代码 · 共 50 行
JAVA
50 行
package cn.edu.nju.software.sd.torm.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.edu.nju.software.sd.torm.PersistenceException;
import cn.edu.nju.software.sd.torm.exception.DatabaseAccessException;
/**
* This is a default implementation of IDGenerator. This Generator
* will generate a id which is 1 larger than the current highest id
* value.
*
* @author Yinfei XU
*
*/
public class IncrementIDGenerator implements IDGenerator {
/* (non-Javadoc)
* @see cn.edu.nju.software.sd.torm.util.IDGenerator#nextID(java.sql.Connection, java.lang.String, java.lang.String)
*/
public int nextID(Connection con, String tableName, String columnName)throws PersistenceException {
PreparedStatement stmt = null;
ResultSet rs = null;
int id = -2;
try {
con.setAutoCommit(false);
stmt = con.prepareStatement("select max("+columnName+") from "+tableName+";");
rs = stmt.executeQuery();
if(rs == null){
throw new DatabaseAccessException("Can't execute the statement : "+stmt.toString());
}
if(rs.next()){
id = rs.getInt(1)+1;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return id;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?