📄 idgeneratorpersistent.java
字号:
/*
* IDGeneratorPersistent.java
* Generated using xgen and texen from beanpersistent.vm
* Thu Jul 25 10:15:26 CST 2002
*/
package com.sure.util.database;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DatabaseMetaData;
import java.sql.BatchUpdateException;
import java.math.BigDecimal;
import com.sure.businessmodel.Page;
import java.util.*;
/**
* (整型)ID生成器
* @author little.-.bird@263.net
*/
public class IDGeneratorPersistent {
public IDGeneratorPersistent(IDGenerator bean) { setBean(bean); }
public IDGeneratorPersistent(IDGenerator bean, boolean recordExists) { setBean(bean); setRecordExists(recordExists); }
/** class attributes */
transient protected boolean recordExists = false;
transient protected boolean handleConcurrency = false;
transient protected boolean handleAutoIncrement = false;
protected IDGenerator bean;
public IDGenerator getBean() { return bean; }
public void setBean(IDGenerator bean) { this.bean = bean; }
public boolean getRecordExists() { return recordExists; }
public void setRecordExists(boolean recordExists) { this.recordExists = recordExists; }
/**
* Load this object from a result set
* If null fields are present in the table, catch the NullPointerException
*/
public static IDGenerator load(ResultSet rs) throws SQLException {
IDGenerator theBean = new IDGenerator ();
try{ theBean.setName ( rs.getString("NAME") ); } catch(NullPointerException e){}
try{ theBean.setValue ( rs.getInt("VALUE") ); } catch(NullPointerException e){}
try{ theBean.setNextValue ( rs.getInt("NEXTVALUE") ); } catch(NullPointerException e){}
try{ theBean.setInitValue ( rs.getInt("INITVALUE") ); } catch(NullPointerException e){}
return theBean;
}
/**
* Load a range of records from a connection, according to the where clause.
* Returns a map of records accessible by a column value. (ie: foreign key)
*/
public static Map loadByKey(Connection conn, String where, String colname)
throws SQLException{
HashMap map = new HashMap();
Statement s = conn.createStatement();
String sql = "SELECT NAME,VALUE,NEXTVALUE,INITVALUE FROM IDTable " + where;
ResultSet rs = s.executeQuery( sql );
while(rs.next()){
IDGenerator theBean = new IDGenerator ();
int col = rs.findColumn(colname);
String key = rs.getString(col);
theBean = load(rs);
map.put(key, theBean);
}
rs.close();
s.close();
return map;
}
/**
* Load a range of records from a connection, according to the where clause
*/
public static Vector load(Connection conn, String where) throws SQLException{
Vector beans = new Vector();
Statement s = conn.createStatement();
String sql = "SELECT NAME,VALUE,NEXTVALUE,INITVALUE FROM IDTable " + where;
ResultSet rs = s.executeQuery( sql );
while(rs.next()){
IDGenerator theBean = new IDGenerator ();
theBean = load(rs);
beans.addElement(theBean);
}
rs.close();
s.close();
return beans;
}
/**
* Load a range of records using a scrollable resultset and absolute cursor
*/
public static Vector load(Connection conn, String where,
int startRow, int rowCount) throws SQLException {
Vector beans = new Vector();
String sql = "SELECT NAME,VALUE,NEXTVALUE,INITVALUE FROM IDTable ";
if(where.length() > 0)
sql = sql + where;
Statement s = conn.createStatement();
int total = startRow + rowCount;
sql = "SELECT * from (SELECT NAME,VALUE,NEXTVALUE,INITVALUE, rownum row_num FROM (" + sql + ") " +
"WHERE rownum<" + total + ") WHERE row_num BETWEEN " + startRow + " AND " + (total-1);
ResultSet rs = s.executeQuery( sql );
if (!rs.next()) return beans;
do {
IDGenerator theBean = new IDGenerator ();
theBean = load(rs);
beans.addElement(theBean);
} while(rs.next());
rs.close();
s.close();
return beans;
}
/**
* Load a Page of records using a scrollable resultset and absolute cursor
*/
public static Page load(Connection conn, int startRow, int pageSize,
String where) throws SQLException {
int count = getCount(conn, where);
if (count == 0) return new Page(pageSize);
startRow = Page.getValidStart(startRow, count, pageSize);
Vector beans = load(conn, where, startRow, pageSize);
return new Page(beans, startRow, count, pageSize);
}
/**
* Persist the record. Performs a sql update or insert.
* The primary key will be "set" upon insert.
* If autocommit is on, turn it off to make a transactional
* check of the tcn field.
*/
public void persist(Connection conn) throws SQLException{
if(recordExists){
if(handleConcurrency){
//regenerate source if you want the bean to handle concurrency
}else{
update(conn);
}
}else{
insert(conn);
}
}
/**
* Insert a new record
*/
public void insert(Connection conn) throws SQLException {
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("INSERT INTO IDTable ( NAME,VALUE,NEXTVALUE,INITVALUE ) VALUES ( ?,?,?,? )");
pstmt.setString ( 1, bean.getName() );
pstmt.setInt ( 2, bean.getValue().intValue() );
pstmt.setInt ( 3, bean.getNextValue().intValue() );
pstmt.setInt ( 4, bean.getInitValue().intValue() );
pstmt.executeUpdate();
pstmt.close();
recordExists = true;
}
/**
* Update an existing record
* (including the primary key)
*/
public void update(Connection conn) throws SQLException{
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("UPDATE IDTable SET " +
"NAME = ?," +
"VALUE = ?," +
"NEXTVALUE = ?," +
"INITVALUE = ? " +
"WHERE name = ?" );
pstmt.setString ( 1, bean.getName () );
pstmt.setInt ( 2, bean.getValue ().intValue() );
pstmt.setInt ( 3, bean.getNextValue ().intValue() );
pstmt.setInt ( 4, bean.getInitValue ().intValue() );
pstmt.setString ( 5, bean.getName () );
pstmt.executeUpdate();
pstmt.close();
}
/**
* Delete an existing record
*/
public static void delete(Connection conn, String where) throws SQLException{
Statement stmt = conn.createStatement();
String sql = "DELETE FROM IDTable " + where;
stmt.execute(sql);
stmt.close();
}
/**
* Delete an existing record using a prepared statement
*/
public void delete(Connection conn) throws SQLException{
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("DELETE FROM IDTable WHERE name = ?");
pstmt.setString( 1, bean.getName () );
pstmt.executeUpdate();
pstmt.close();
}
/**
* get total count of a range of records from a connection, according to the where clause
*/
protected static int getCount(Connection conn, String where) throws SQLException{
Statement s = conn.createStatement();
String sql = "SELECT count(NAME) FROM IDTable ";
if(where.length() > 0)
sql = sql + where;
ResultSet rs = s.executeQuery( sql );
int count = 0;
if( rs.next()){
count = rs.getInt(1);
}
rs.close();
s.close();
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -