⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 incrementidgenerator.java

📁 软件设计课做的一个类似Hibernate的O/R Mapping的框架
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -