📄 gddbcommon.java
字号:
String classVo = "";
String table = strStart.substring(0, endPos).toUpperCase();//取得表名,并转换为大写
//和XMl中设定的表名进行对比
for (int i = 0; voId.size() > i; i++) {
String strTable = voTable.get(i) == null ? "" : (String)voTable.get(i);
if (table.equals(strTable.toUpperCase())) {//如果该sql中的表名和Xml中的相对应,则去对该表对应的Class,即VO
classVo = (String)voClass.get(i);
}
}
Object obj = null;
List columnNamesList = new ArrayList();
int columnCount = 0;
try {
columnCount = rs.getMetaData().getColumnCount();//获取ResultSetMetaData的字段数目
columnNamesList = setColumnNameByMeta(rs.getMetaData());//获取每个字段的名称
Class cls = Class.forName(classVo);
obj = (Object)cls.newInstance();//取得该Class即VO的实例
Method[] mtdVos = cls.getMethods();//获取该VO中的所有方法
for (int i = 0; i < columnCount; i++) {
//对该VO中的所有方法进行循环,如果方法名和表中的字段名一致,且该方法为set方法,则将值注入
for (int m = 0; mtdVos.length > m; m++) {
if ("set".equals(mtdVos[m].getName().substring(0, 3)) && ((String) columnNamesList.get(i)).toUpperCase().equals(mtdVos[m].getName().substring(3).toUpperCase())) {
Method mtdVo = cls.getMethod(mtdVos[m].getName(), new Class[]{String.class});
mtdVo.invoke(obj, rs.getString(i + 1));//将数据库中的值注入VO
}
}
}
} catch (SQLException e) {
throw new SQLException("执行getObjFromRS失败" + e);
} finally {
return obj;
}
}
/**
* 该方法用来将获取的栏位名称和栏位内容相对应
*
* @param rs
* @return Map
*/
private Map getMapFromRs(CachedRowSet rs) throws SQLException {
Map map = new HashMap();
List columnNamesList = new ArrayList();
int columnCount = 0;
try {
columnCount = rs.getMetaData().getColumnCount();//获取ResultSetMetaData的字段数目
columnNamesList = setColumnNameByMeta(rs.getMetaData());//获取每个字段的名称
for (int i = 0; i < columnCount; i++) {
map.put((String) columnNamesList.get(i), rs.getString(i + 1));//将字段名和对应的值存入Map
}
} catch (SQLException e) {
throw new SQLException("执行getMapFromRS失败" + e);
} finally {
return map;
}
}
/**
* 该方法用来获取处理数据库中的栏位名和栏位数目
* @param rsMetadata
* @return List
*/
private List setColumnNameByMeta(ResultSetMetaData rsMetadata) throws SQLException {
List columnNamesList = new ArrayList();
try {
//获取ResultSetMetaData的字段数目
for (int i = 0; i < rsMetadata.getColumnCount(); i++) {
columnNamesList.add(rsMetadata.getColumnName(i + 1));//获取每个字段的名称
}
} catch (SQLException e) {
throw new SQLException("执行setColumnNameByMeta失败" + e);
} finally {
return columnNamesList;
}
}
/**用来设定PreparedStatement的时间戳参数
* @param parameterIndex
* @param x
* @param cal
* @throws java.sql.SQLException
*/
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
throws SQLException {
pstmt.setTimestamp(parameterIndex, x, cal);
}
/**用来设定PreparedStatement的Short型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setShort(int parameterIndex, short x) throws SQLException {
pstmt.setShort(parameterIndex, x);
}
/**用来设定PreparedStatement的Long型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setLong(int parameterIndex, long x) throws SQLException {
pstmt.setLong(parameterIndex, x);
}
/**用来获取PreparedStatement的Fectch Size 的大小
* @return int
* @throws java.sql.SQLException
*/
public int getFetchSize() throws SQLException {
return pstmt.getFetchSize();
}
/**用来设定PreparedStatement的字符型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setString(int parameterIndex, String x) throws SQLException {
pstmt.setString(parameterIndex, x);
}
/**
* @throws java.sql.SQLException
*/
public void cancel() throws SQLException {
pstmt.cancel();
}
/**用来设定PreparedStatement的Object型的参数,根据类型名和范围
* @param parameterIndex
* @param x
* @param targetSqlType
* @param scale
* @throws java.sql.SQLException
*/
public void setObject(int parameterIndex, Object x, int targetSqlType,
int scale) throws SQLException {
pstmt.setObject(parameterIndex, x, targetSqlType, scale);
}
/**用来设定PreparedStatement的日期型的参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setDate(int parameterIndex, Date x) throws SQLException {
pstmt.setDate(parameterIndex, x);
}
/**用来设定PreparedStatement的Ascii流型的参数
* @param parameterIndex
* @param x
* @param length
* @throws java.sql.SQLException
*/
public void setAsciiStream(int parameterIndex, InputStream x, int length)
throws SQLException {
pstmt.setAsciiStream(parameterIndex, x, length);
}
/**用来设定PreparedStatement中参数的null值,根据类型名
* @param paramIndex
* @param sqlType
* @param typeName
* @throws java.sql.SQLException
*/
public void setNull(int paramIndex, int sqlType, String typeName)
throws SQLException {
pstmt.setNull(paramIndex, sqlType, typeName);
}
/**用来设定PreparedStatement的Fetch Size的大小,表示每次从数据库中读取的数量
* @param rows
* @throws java.sql.SQLException
*/
public void setFetchSize(int rows) throws SQLException {
pstmt.setFetchSize(rows);
}
/**用来设定PreparedStatement的日期型参数
* @param parameterIndex
* @param x
* @param cal
* @throws java.sql.SQLException
*/
public void setDate(int parameterIndex, Date x, Calendar cal)
throws SQLException {
pstmt.setDate(parameterIndex, x, cal);
}
/**用来设定PreparedStatement的整型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setInt(int parameterIndex, int x) throws SQLException {
pstmt.setInt(parameterIndex, x);
}
/**用来设定PreparedStatement的字节型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
pstmt.setBytes(parameterIndex, x);
}
/**用来设定PreparedStatement的字节流型参数
* @param parameterIndex
* @param reader
* @param length
* @throws java.sql.SQLException
*/
public void setCharacterStream(int parameterIndex, Reader reader, int length)
throws SQLException {
pstmt.setCharacterStream(parameterIndex, reader, length);
}
/**用来设定PreparedStatement的Double型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setDouble(int parameterIndex, double x) throws SQLException {
pstmt.setDouble(parameterIndex, x);
}
/**用来设定PreparedStatement中参数的null值
* @param parameterIndex
* @param sqlType
* @throws java.sql.SQLException
*/
public void setNull(int parameterIndex, int sqlType) throws SQLException {
pstmt.setNull(parameterIndex, sqlType);
}
/**用来设定PreparedStatement的URL型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setURL(int parameterIndex, URL x) throws SQLException {
pstmt.setURL(parameterIndex, x);
}
/**用来设定PreparedStatement的Object型参数,根据目标的类型
* @param parameterIndex
* @param x
* @param targetSqlType
* @throws java.sql.SQLException
*/
public void setObject(int parameterIndex, Object x, int targetSqlType)
throws SQLException {
pstmt.setObject(parameterIndex, x, targetSqlType);
}
/**用来设定PreparedStatement的二进制型参数
* @param parameterIndex
* @param x
* @param length
* @throws java.sql.SQLException
*/
public void setBinaryStream(int parameterIndex, InputStream x, int length)
throws SQLException {
pstmt.setBinaryStream(parameterIndex, x, length);
}
/**用来设定PreparedStatement的Boolean型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
pstmt.setBoolean(parameterIndex, x);
}
/**用来设定PreparedStatement的BigDecimal型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setBigDecimal(int parameterIndex, BigDecimal x)
throws SQLException {
pstmt.setBigDecimal(parameterIndex, x);
}
/**用来设定PreparedStatement的Float型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setFloat(int parameterIndex, float x) throws SQLException {
pstmt.setFloat(parameterIndex, x);
}
/**用来设定PreparedStatement的时间型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setTime(int parameterIndex, Time x) throws SQLException {
pstmt.setTime(parameterIndex, x);
}
/**用来设定PreparedStatement的字节型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setByte(int parameterIndex, byte x) throws SQLException {
pstmt.setByte(parameterIndex, x);
}
/**用来设定PreparedStatement的时间型参数
* @param parameterIndex
* @param x
* @param cal
* @throws java.sql.SQLException
*/
public void setTime(int parameterIndex, Time x, Calendar cal)
throws SQLException {
pstmt.setTime(parameterIndex, x, cal);
}
/**用来设定PreparedStatement的Object型参数
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setObject(int parameterIndex, Object x) throws SQLException {
pstmt.setObject(parameterIndex, x);
}
/**用来设定PreparedStatement的时间戳型参数,INSERT INTO User(username, CREATEDATETIME) VALUES(?,CURRENT TIMESTAMP)
* @param parameterIndex
* @param x
* @throws java.sql.SQLException
*/
public void setTimestamp(int parameterIndex, Timestamp x)
throws SQLException {
pstmt.setTimestamp(parameterIndex, x);
}
/**
* 用来通过PreparedStatement设定多笔数据结果集
* @param sql
* @param limit,用来限制查询笔数,<=0则代表不限制
* @param gk,用来自动生成主键值,>0表示自动生成主键
* @throws SQLException
*/
public void PreparedStatement(String sql, int limit, int gk) throws SQLException{
this.sql = sql;
pstmt = null;
// 判断连接是否为空或已关闭
if (conn == null || conn.isClosed())
throw new SQLException("请首先创建或获取一个连接");
//判断传进来的sql是否为空
if (sql == null || "".equals(sql.trim()))
throw new SQLException("sql为null");
try {
synchronized (this.conn) {//用来保证一个对象在多个线程中访问该方法时是线程安全的
if (gk > 0) {
pstmt = this.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);//自动生成主键
} else {
pstmt = this.conn.prepareStatement(sql);//不自动生成主键
}
}
if (limit > 0) {
pstmt.setMaxRows(limit);//限制获取数据的笔数
}
} catch (SQLException e) {
throw new SQLException("执行PreparedStatement失败" + sql + e);
}
}
/**
* 返回自动生成的主键值
* @return
* @throws SQLException
*/
public int getGeneratedKeys() throws SQLException {
ResultSet rs = pstmt.getGeneratedKeys();
int gk = Integer.MIN_VALUE;
if(rs.next())
gk = rs.getInt(1);
rs.close();
rs = null;
return gk;
}
/**
* 用来通过PreparedStatement设定多笔数据结果集
* @param sql
* @param limit,用来限制查询笔数,<=0则代表不限制
* @return void
* @throws SQLException
*/
public void PreparedStatement(String sql, int limit) throws SQLException{
this.PreparedStatement(sql, limit, 0);
}
/**
* 用来通过PreparedStatement设定数据结果集
* @param sql
* @return void
* @throws SQLException
*/
public void PreparedStatement(String sql) throws SQLException{
this.PreparedStatement(sql, 0);
}
/**用来执行新增、修改、删除操作
* @return int
* @throws java.sql.SQLException
*/
public int executeUpdate() throws SQLException {
return pstmt.executeUpdate();
}
/**用来增加Batch
* @throws java.sql.SQLException
*/
public void addBatch() throws SQLException {
pstmt.addBatch();
}
/**用来执行Batch
* @return int[],即执行成功的数目
* @throws java.sql.SQLException
*/
public int[] executeBatch() throws SQLException {
return pstmt.executeBatch();
}
/**清空Batch
* @throws java.sql.SQLException
*/
public void clearBatch() throws SQLException {
pstmt.clearBatch();
}
/**获得设定的infoIn
* @return the infoIn
*/
public InfoInAndOut getInfoIn() {
return infoIn;
}
/**设定infoIn
* @param infoIn the infoIn to set
*/
public void setInfoIn(InfoInAndOut infoIn) {
this.infoIn = infoIn;
}
/**设定conn
* @return the conn
*/
public Connection getConn() {
return conn;
}
/**获得conn
* @param conn the conn to set
*/
public void setConn(Connection conn) {
this.conn = conn;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -