com.san.strutsAjax.utils.EngenderID (主鍵生成類)
package com.san.strutsAjax.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
/**
* Created by IntelliJ IDEA.
* User: huxinsheng
* Date: 2006-4-9
* Time: 15:41:39
* To change this template use File | Settings | File Templates.
*/
public class EngenderID {
public static int engenderTableId(String table) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet res = null;
int i = 0;
String sql = "select max(id)as id from " + table;
try {
conn = ConnectionDB.ConnectToDb();
pstmt = conn.prepareStatement(sql);
res = pstmt.executeQuery();
while(res.next()){
i = res.getInt("id");
}
if (i == 0) {
return 1;
} else {
return ++i;
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
throw new Exception(e);
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}