📄 basedaoimpl.java
字号:
package com.icss.oa.base.impl;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.icss.oa.base.BaseDAO;
import com.icss.oa.utils.ConnectionFactory;
import com.icss.oa.utils.DatabaseUtils;
public class BaseDAOImpl implements BaseDAO {
private Connection conn = null;
private PreparedStatement pstm = null;
private ResultSet rs = null;
public boolean executeObject(String sql, Object[] objs) {
boolean flag = false;
try {
conn = ConnectionFactory.getConnectionByJNDI();
pstm = conn.prepareStatement(sql);
for(int i = 0, j=1 ;i<objs.length;i++,j++){
Object obj = objs[i];
Type type = (Type) obj.getClass();
if(type==String.class){
pstm.setString(j,obj.toString());
}else if(type==Integer.class){
pstm.setLong(j,(Integer)obj);
}else if(type==Long.class){
pstm.setLong(j,(Integer)obj);
}else if(type==Date.class){
pstm.setDate(j, (Date)obj);
}else if(type==Timestamp.class){
pstm.setTimestamp(j, (Timestamp)obj);
}
}
pstm.executeUpdate();
flag = true;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}finally{
DatabaseUtils.release(rs, pstm, conn);
}
return flag;
}
public boolean executeObject(String sql) {
boolean flag = false;
try {
conn = ConnectionFactory.getConnectionByJNDI();
pstm = conn.prepareStatement(sql);
pstm.executeUpdate();
flag = true;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}finally{
DatabaseUtils.release(rs, pstm, conn);
}
return flag;
}
public List queryAllObject(String strQuery, Object[] objs) {
List lists = new ArrayList();
Properties prop = null;
try{
conn = ConnectionFactory.getConnectionByJNDI();
pstm =conn.prepareStatement(strQuery);
for(int i = 0, j=1 ;i<objs.length;i++,j++){
Object obj = objs[i];
System.out.println("obj:"+obj);
Type type = (Type) obj.getClass();
if(type==String.class){
pstm.setString(j,obj.toString());
}else if(type==Integer.class){
pstm.setLong(j,(Integer)obj);
}else if(type==Long.class){
pstm.setLong(j,(Integer)obj);
}else if(type==Date.class){
pstm.setDate(j, (Date)obj);
}else if(type==Timestamp.class){
pstm.setTimestamp(j, (Timestamp)obj);
}
}
rs = pstm.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int countColumn = rsmd.getColumnCount();
while(rs.next()){
prop = new Properties();
for(int columnIndex= 1;columnIndex<=countColumn;columnIndex++){
prop.put(rsmd.getColumnName(columnIndex),rs.getString(rsmd.getColumnName(columnIndex)));
}
lists.add(prop);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("release===1===="+conn);
DatabaseUtils.release(rs, pstm, conn);
}
return lists;
}
public Properties querySingleObject(String strQuery) {
Properties prop = new Properties();
try{
conn = ConnectionFactory.getConnectionByJNDI();
pstm = conn.prepareStatement(strQuery);
rs = pstm.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int countColumn = rsmd.getColumnCount();
if(rs.next()){
for(int columnIndex= 1;columnIndex<=countColumn;columnIndex++){
prop.put(rsmd.getColumnName(columnIndex),rs.getString(rsmd.getColumnName(columnIndex)));
//properties.put(key,value);
// .put("USERNAME","che100");
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}finally{
DatabaseUtils.release(rs, pstm, conn);
}
return prop;
}
public Properties querySingleObject(String strQuery, Object[] objs) {
Properties prop = new Properties();
try{
conn = ConnectionFactory.getConnectionByJNDI();
pstm = conn.prepareStatement(strQuery);
for(int i = 0, j=1 ;i<objs.length;i++,j++){
Object obj = objs[i];
System.out.println("obj:"+obj);
Type type = (Type) obj.getClass();
if(type==String.class){
pstm.setString(j,obj.toString());
}else if(type==Integer.class){
pstm.setLong(j,(Integer)obj);
}else if(type==Long.class){
pstm.setLong(j,(Integer)obj);
}else if(type==Date.class){
pstm.setDate(j, (Date)obj);
}else if(type==Timestamp.class){
pstm.setTimestamp(j, (Timestamp)obj);
}
}
rs = pstm.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int countColumn = rsmd.getColumnCount();
if(rs.next()){
for(int columnIndex= 1;columnIndex<=countColumn;columnIndex++){
prop.put(rsmd.getColumnName(columnIndex),rs.getString(rsmd.getColumnName(columnIndex)));
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DatabaseUtils.release(rs, pstm, conn);
}
return prop;
}
public List queryAllObject(String strQuery) {
List lists = new ArrayList();
Properties prop = null;
try{
pstm = ConnectionFactory.getConnectionByJNDI().prepareStatement(strQuery);
rs = pstm.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int countColumn = rsmd.getColumnCount();
while(rs.next()){
prop = new Properties();
for(int columnIndex= 1;columnIndex<=countColumn;columnIndex++){
prop.put(rsmd.getColumnName(columnIndex),rs.getString(rsmd.getColumnName(columnIndex)));
}
lists.add(prop);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DatabaseUtils.release(rs, pstm, conn);
}
return lists;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -