package com.zfsoft.setup.dao.impl;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.driver.OracleTypes;
import com.zfsoft.util.Pages;
import com.zfsoft.util.StringFormat;
import com.zfsoft.util.Tool;
/**
* 数据库操作基本类
*
*/
public class DAO {
/**
* 数据源
*/
//protected static DataSource db;
/**
* 连接
*/
protected Connection conn = null;
protected PreparedStatement stmt = null;
protected Statement stat = null;
protected CallableStatement cstmt = null;
protected ResultSet rs = null;
protected PreparedStatement ps = null;
private ResultSetMetaData rsmd = null;
// public int print = 0;
private String splitSignOne = "!!SplitSignOne!!";
private String splitSignTwo = "!!SplitSignTwo!!";
// /**
// * 实例化DAO
// *
// */
// public DAO() {
// this.db = DBPool.getPool();
// }
//
// /**
// * 实例化DAO
// *
// */
// public DAO(int print) {
// this.db = DBPool.getPool();
// this.print = print;
// }
public DAO(Connection conn){
this.conn= conn;
}
private DAO(){
}
/**
* 功能描述:返回数组 里面包含一列outputValue 作者:张建军; 日期:2008-6-30 日期:上午08:25:35
* 方法名:getOneRs 访问类:DAO
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param outputValue
* 输出字段数组
* @return String[]
*
*/
public String[] getOneRs(String sql, String[] inputValue, String[] outputValue)
{
String[] result = new String[outputValue.length];// SIZE
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
for (int i = 0; i < inputValue.length; i++)
{
stmt.setString(i + 1, inputValue[i]);
}
rs = stmt.executeQuery();
if (rs.next())
{
for (int i = 0; i < outputValue.length; i++)
{
String temp = rs.getString(outputValue[i]);
result[i] = Tool.isNull(temp) ? "" : temp;
}
}
else
{
result = null;
}
closeStmt();
}
catch (SQLException e)
{
e.printStackTrace();
Tool.print("sql:" + sql);
for (int i = 0; i < inputValue.length; i++)
{
if (i == inputValue.length - 1)
{
Tool.print(inputValue[i]);
}
else
{
System.out.print(inputValue[i] + " ");
}
}
result = null;
}
finally
{
closeStmt();
closeConn();
}
return result;
}
/**
*
* 功能描述:返回字符串 里面包含一列outputValue 作者:张建军; 日期:2008-6-30 日期:上午08:32:55
* 方法名:getOneRs 访问类:DAO
*
* @param sql
* 语句
* @return String
*
*/
public String getOneRs(String sql)
{
String result;
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
if (rs.next())
{
result = rs.getString(1);
}
else
{
result = null;
}
closeStmt();
}
catch (SQLException e)
{
e.printStackTrace();
Tool.print("sql:" + sql);
result = null;
}
finally
{
closeStmt();
closeConn();
}
return result;
}
/**
*
* 功能描述:关闭Stmt 作者:张建军; 日期:2008-6-30 日期:上午08:40:16 方法名:closeStmt 访问类:DAO
*
*/
protected void closeStmt()
{
try
{
if (rs != null)
{
this.rs.close();
}
if (ps != null)
{
this.ps.close();
}
if (stmt != null)
{
this.stmt.close();
}
if (stat != null)
{
this.stat.close();
}
if (cstmt != null)
{
this.cstmt.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
}
}
/**
*
*
*/
public void closeConn()
{
}
public void closeConnManual()
{
try
{
closeStmt();
if (conn != null)
{
this.conn.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
this.rs = null;
this.ps = null;
this.stmt = null;
this.stat = null;
this.cstmt = null;
this.conn = null;
}
}
public String[] getColumnName(String sql)
{
String[] tit = null;
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
rsmd = rs.getMetaData();
tit = new String[rsmd.getColumnCount()];
for (int i = 0; i < rsmd.getColumnCount(); i++)
{
tit[i] = rsmd.getColumnLabel(i + 1);
}
closeStmt();
}
catch (SQLException e)
{
e.printStackTrace();
Tool.print(sql);
tit = new String[0];
}
finally
{
closeStmt();
closeConn();
}
return tit;
}
public String[] getColumnNameCN(String[] cName, String tabName, String Dblink)
{
String[] tit = new String[cName.length];
String sql = "";
sql = "select COMMENTS from user_col_comments where table_name=? and COLUMN_NAME=?";
for (int i = 0; i < cName.length; i++)
{
String[] tmp = getOneRs(sql, new String[] { tabName.toUpperCase(), cName[i].toUpperCase() },
new String[] { "COMMENTS" });
if (tmp == null)
{
tit[i] = cName[i];
}
else
{
tit[i] = tmp[0];
if ((tit[i] == null) || tit[i].toLowerCase().equalsIgnoreCase("null") || tit[i].equalsIgnoreCase(""))
{
tit[i] = cName[i];
}
if (tit[i].indexOf('(') >= 0)
{
tit[i] = tit[i].substring(0, tit[i].indexOf('('));
}
}
}
return tit;
}
/**
*
* 访问类:DAO
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param outputValue
* 输出字段数组
* @return HashMap
*
*/
public HashMap<String, String> getOneMapByList(String sql, String[] inputValue, String[] outputValue)
{
List<HashMap<String, String>> list = this.getList(sql, inputValue, outputValue);
if (list.size() == 0)
return null;
else
return (HashMap<String, String>) list.get(0);
}
/**
*
* 方法名:getList 访问类:DAO
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param outputValue
* 输出字段数组
* @return List
*
*/
public List<HashMap<String, String>> getList(String sql, String[] inputValue, String[] outputValue)
{
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
for (int i = 0; i < inputValue.length; i++)
{
stmt.setString(i + 1, inputValue[i]);
}
rs = stmt.executeQuery();
while (rs.next())
{
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < outputValue.length; i++)
{
String temp = rs.getString(outputValue[i]);
map.put(outputValue[i], Tool.isNull(temp) ? "" : temp);
}
arrayList.add(map);
}
closeStmt();
}
catch (SQLException e)
{
Tool.print(e.getMessage());
Tool.print("sql:" + sql);
for (int i = 0; i < inputValue.length; i++)
{
if (i == inputValue.length - 1)
{
Tool.print(inputValue[i]);
}
else
{
System.out.print(inputValue[i] + " ");
}
}
// e.printStackTrace();
arrayList = null;
}
finally
{
closeStmt();
closeConn();
}
return arrayList;
}
/**
*
*
* @param sql
* 语句
* @return List
* @throws SQLException
*
*/
public List<String> getRealRow(String sql) throws SQLException
{
List<String> list = new ArrayList<String>();
try
{
//this.conn = db.getConnection();
stat = conn.createStatement();
rs = stat.executeQuery(sql);
java.sql.ResultSetMetaData rsdate = rs.getMetaData();
int allrows = rsdate.getColumnCount();
for (int i = 1; i <= allrows; i++)
{
// rstring[i-1]=rsdate.getColumnLabel(i);
list.add(rsdate.getColumnLabel(i));
}
closeStmt();
return list;
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
throw e;
}
finally
{
closeStmt();
closeConn();
}
}
/**
* throw error
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param outputValue
* 输出字段数组
* @return List
* @throws SQLException
*/
public List<HashMap<String, String>> getnewList(String sql, String[] inputValue, String[] outputValue)
throws SQLException
{
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
for (int i = 0; i < inputValue.length; i++)
{
stmt.setString(i + 1, inputValue[i]);
}
rs = stmt.executeQuery();
while (rs.next())
{
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < outputValue.length; i++)
{
String temp = rs.getString(outputValue[i]);
map.put(outputValue[i], Tool.isNull(temp) ? "" : temp);
}
arrayList.add(map);
}
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
for (int i = 0; i < inputValue.length; i++)
{
if (i == inputValue.length - 1)
{
Tool.print(inputValue[i]);
}
else
{
System.out.print(inputValue[i] + " ");
}
}
arrayList = null;
throw e;
}
finally
{
closeStmt();
closeConn();
}
return arrayList;
}
/**
* 功能描述:返回一行数据当中一列的值 例如 select * from car where id=?/"123"/name 作者:张建军;
* 日期:2008-6-30 日期:上午08:52:30 方法名:getOneRs 访问类:DAO
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param outputValue
* 返回字段名
* @return String
*
*/
public String getOneRs(String sql, String[] inputValue, String outputValue)
{
String[] result = getOneRs(sql, inputValue, new String[] { outputValue });// return
// one
// row
if (result != null && result.length == 1)
{
return result[0];
}
return "";
}
/**
*
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param outputValue
* 输出字段数组
* @return Vector<String[]>
*
*/
public Vector<String[]> rsToVator(String sql, String[] inputValue, String[] outputValue)
{
Vector<String[]> vector = new Vector<String[]>();
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
for (int i = 0; i < inputValue.length; i++)
{
stmt.setString(i + 1, inputValue[i]);
}
rs = stmt.executeQuery();
while (rs.next())
{
String[] tmp = new String[outputValue.length];
for (int i = 0; i < outputValue.length; i++)
{
tmp[i] = rs.getString(outputValue[i]);
if ((tmp[i] == null) || tmp[i].equalsIgnoreCase("null"))
{
tmp[i] = "";
}
}
vector.add(tmp);
}
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
for (int i = 0; i < inputValue.length; i++)
{
if (i == inputValue.length - 1)
{
Tool.print(inputValue[i]);
}
else
{
System.out.print(inputValue[i] + " ");
}
}
e.printStackTrace();
vector = null;
}
finally
{
closeStmt();
closeConn();
}
return vector;
}
/**
*
* @param sql
* 语句
* @param input
* 传参数数组
* @return boolean
*
*/
public boolean runUpdate(String sql, String[] input)
{
boolean result = true;
try
{
//this.conn = db.getConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
for (int i = 0; i < input.length; i++)
{
stmt.setString(i + 1, input[i]);
}
stmt.executeUpdate();
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
e.printStackTrace();
try
{
conn.rollback();
}
catch (SQLException e1)
{
Tool.print("sql:" + sql);
e.printStackTrace();
}
result = false;
}
finally
{
try
{
conn.commit();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
e.printStackTrace();
}
closeStmt();
closeConn();
}
return result;
}
/**
*
*
* @param sql
* 语句
* @return boolean
*
*/
public boolean runUpdate(String sql)
{
boolean result = true;
try
{
//this.conn = db.getConnection();
conn.setAutoCommit(false);
stat = conn.createStatement();
stat.execute(sql);
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
e.printStackTrace();
try
{
conn.rollback();
}
catch (SQLException e1)
{
Tool.print("sql:" + sql);
e.printStackTrace();
}
result = false;
}
finally
{
try
{
conn.commit();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
e.printStackTrace();
}
closeStmt();
closeConn();
}
return result;
}
/**
*
* 访问类:DAO
*
* @param sql
* 语句
* @param input
* 传参数数组
* @return String
*
*/
public String runewUpdate(String sql, String[] input)
{
String result = "";
try
{
//this.conn = db.getConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
for (int i = 0; i < input.length; i++)
{
stmt.setString(i + 1, input[i]);
}
stmt.executeUpdate();
closeStmt();
}
catch (SQLException e)
{
result = e.getMessage();
Tool.print("sql:" + sql);
// e.printStackTrace();
try
{
conn.rollback();
}
catch (SQLException e1)
{
result = e.getMessage();
Tool.print("sql:" + sql);
// e.printStackTrace();
}
// result = false;
}
finally
{
try
{
conn.commit();
}
catch (SQLException e)
{
result = e.getMessage();
Tool.print("sql:" + sql);
// e.printStackTrace();
}
closeStmt();
closeConn();
}
return result;
}
/**
*
* 功能描述:重写UPDATE 得到错误信息 return==null 说明 执行成功
* 日期:上午09:03:18 方法名:runewUpdate 访问类:DAO
*
* @param sql
* 语句
* @return String
*
*/
public String runewUpdate(String sql)
{
String result = null;
try
{
//this.conn = db.getConnection();
conn.setAutoCommit(false);
stat = conn.createStatement();
stat.execute(sql);
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
// Tool.print("错误信息::::::::::::" + e.getMessage());
try
{
conn.rollback();
}
catch (SQLException e1)
{
Tool.print("sql:" + sql);
// Tool.print("错误信息::::::::::::" + e.getMessage());
}
result = e.getMessage();
}
finally
{
try
{
conn.commit();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
// Tool.print("错误信息::::::::::::" + e.getMessage());
}
closeStmt();
closeConn();
}
return result;
}
/**
*
* 功能描述:执行语句返回真假
*
* @param v
* 语句集合
* @return boolean
*
*/
public boolean runUpdate(Vector<HashMap<String, Object>> v)
{
String sqlTxt = "";
String sqlVal = "";
boolean result = true;
try
{
//this.conn = db.getConnection();
conn.setAutoCommit(false);
for (int i = 0; i < v.size(); i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map = (HashMap<String, Object>) v.get(i);
String[] inVal = (String[]) map.get("sqlVal");
stmt = conn.prepareStatement(map.get("sqlTxt").toString());
for (int j = 0; j < inVal.length; j++)
{
sqlTxt = map.get("sqlTxt").toString();
sqlVal +=inVal[j]+",";
stmt.setString(j + 1, inVal[j]);
}
stmt.executeUpdate();
sqlVal = "";
closeStmt();
}
}
catch (SQLException e)
{
e.printStackTrace();
try
{
System.out.println("错误的语句为:");
System.out.println(sqlTxt);
System.out.println(sqlVal);
conn.rollback();
}
catch (SQLException e1)
{
e.printStackTrace();
}
result = false;
}
finally
{
try
{
conn.commit();
}
catch (SQLException e)
{
e.printStackTrace();
}
closeStmt();
closeConn();
}
return result;
}
public PreparedStatement geStatement(String sql)
{
return stmt;
}
/**
*
* 功能描述:执行存储过程
*
* @param sql
* 语句
* @param input
* 传参数数组
* @return boolean
*
*/
public boolean runProcuder(String sql, String[] input)
{
boolean result = true;
try
{
//this.conn = db.getConnection();
cstmt = conn.prepareCall(sql);
for (int i = 0; i < input.length; i++)
{
cstmt.setString(i + 1, input[i]);
}
cstmt.executeUpdate();
closeStmt();
}
catch (SQLException e)
{
e.printStackTrace();
try
{
conn.rollback();
}
catch (SQLException e1)
{
e.printStackTrace();
}
result = false;
}
finally
{
try
{
conn.commit();
}
catch (SQLException e)
{
e.printStackTrace();
}
closeStmt();
closeConn();
}
return result;
}
/**
* 功能描述:实现分页查询,需要存储过程
* 访问类:DAO 返回类型:List<HashMap<String,String>>
*
* @param sqlselect
* 查询语句
* @param outputValue
* 输出字段数组
* @param pages
* 分页对象
* @return List
*/
public List<HashMap<String, String>> runProcuderPage(String sqlselect, String[] outputValue, Pages pages)
{
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
try
{
//this.conn = db.getConnection();
String sql = "{ call prc_query(?,?,?,?,?,?)}";
cstmt = conn.prepareCall(sql);
cstmt.setString(1, sqlselect);
cstmt.setInt(2, pages.getCurrentPage());
cstmt.setInt(3, pages.getPageSize());
cstmt.registerOutParameter(4, OracleTypes.INTEGER);
cstmt.registerOutParameter(5, OracleTypes.INTEGER);
cstmt.registerOutParameter(6, OracleTypes.CURSOR);
cstmt.execute();
pages.setMaxRecord(cstmt.getInt(4));
pages.setMaxPage(cstmt.getInt(5));
rs = (ResultSet) cstmt.getObject(6);
while (rs.next())
{
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < outputValue.length; i++)
{
map.put(outputValue[i], rs.getString(outputValue[i]) == null ? "" : rs.getString(outputValue[i]));
}
arrayList.add(map);
}
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sqlselect);
e.printStackTrace();
try
{
conn.rollback();
}
catch (SQLException e1)
{
Tool.print("sql:" + sqlselect);
e.printStackTrace();
}
}
finally
{
try
{
conn.commit();
}
catch (SQLException e)
{
Tool.print("sql:" + sqlselect);
e.printStackTrace();
}
closeStmt();
closeConn();
}
return arrayList;
}
/**
*
* 功能描述:读取Clob类型列 返回Clob类型
* 访问类:DAO
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param colName
* 列名
* @return Clob
*
*/
public Clob getOneClob(String sql, String[] inputValue, String colName)
{
Clob clob = null;
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
for (int i = 0; i < inputValue.length; i++)
{
stmt.setString(i + 1, inputValue[i]);
}
rs = stmt.executeQuery();
rs.next();
clob = rs.getClob(colName);
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
e.printStackTrace();
}
finally
{
closeStmt();
closeConn();
}
return clob;
}
/**
*
* 功能描述:返回Clob类型列,返回二进制流byte[]
* 方法名:getOneBlob 访问类:DAO
*
* @param sql
* 语句
* @param inputValue
* 传入参数数组
* @param colName
* 列名
* @return byte[]
*
*/
public byte[] getOneBlob(String sql, String[] inputValue, String colName)
{
byte[] blob = null;
try
{
//this.conn = db.getConnection();
stmt = conn.prepareStatement(sql);
for (int i = 0; i < inputValue.length; i++)
{
stmt.setString(i + 1, inputValue[i]);
}
rs = stmt.executeQuery();
while (rs.next())
{
blob = ((OracleResultSet) rs).getBytes(colName);
if (rs.wasNull())
{
blob = null;
}
}
closeStmt();
}
catch (SQLException e)
{
Tool.print("sql:" + sql);
e.printStackTrace();
}
finally
{
closeStmt();
closeConn();
}
return blob;
}
/**
*
* 功能描述:得到连接池
*
* @return Connection
*/
public Connection getConn()
{
return conn;
}
public Connection getDBConn()
{
return conn;
}
public String getSplitSignOne()
{
return splitSignOne;
}
public String getSplitSignTwo()
{
return splitSignTwo;
}
/**
*
* 功能描述:得到sql语句总数
*
* @param sql
* 语句
* @return String
*
*/
public String getSQLCount(String sql)
{
String sql_temp = "select count(*) count from ( " + sql + " )";
return sql_temp;
}
/**
* 获得表table的row列的最大值 to_number alreadly row列没有string 值通常为自动增长的int int
* max=dao.getTableMaxCountRow("car_use","c_id");
*
* @param table
* 表名
* @param row
* 字段名
* @return int
*/
public int getTableMaxCountRow(String table, String row)
{
String sql = "select max(to_number(" + row + ")) max from " + table;
String max = this.getOneRs(sql);
return Integer.parseInt(max);
}
/**
* 查询在table表里 row列中 value值是否存在 存在返回true 不存在返回false
* boolean=dao.checkBeing("car_info","c_no","AA"); *
*
* @param table
* 表名
* @param row
* 字段
* @param value
* 参数
* @return boolean
*/
public boolean checkBeing(String table, String row, String value)
{
String sql = "select * from " + table + " where " + row + "=?";
boolean bool = true;
// ResultSet rs = this.getRS(sql, new String[] { value });// 查询出结果
List<HashMap<String, String>> list = this.getList(sql, new String[] { value }, new String[0]);
if (list.size() > 0)// 有next说明存在这一列
bool = true;
else
bool = false;
return bool;
}
/**
* 删除表table中 row列值为value的列 WARNING:注意此列是否是唯一(del all)}
* dao.deleteTableRow("car_info","c_no","AA");
*
* @param table
* 表名
* @param row
* 字段名
* @param value
* 参数
* @return boolean
*/
public boolean deleteTableRow(String table, String row, String value)
{
boolean bool = true;
String sql = "delete " + table + " where " + row + "=?";
bool = this.runUpdate(sql, new String[] { value });
return bool;
}
/**
* 插入表数据 已经处理危险字符 直接传入字符串即可
*
* String table = "car_info"; String[] row = new String[] { "c_no",
* "c_name", "c_MASS" }; String[] value = new String[] { "vvvvvvAAAA",
* "sssss", "52" }; int [] length=new int[]{20,50,80};
*
* dao.insert(table, row, value,length);
*
* @param table
* 表名字
* @param row
* 待插入的列 into(c_no,c_name)
* @param value
* 待插入的值 "AAA" "VVV"
* @param length
* length 字符串长度
* @return boolean
*/
public boolean insert(String table, String[] row, String[] value, int[] length)
{
boolean bool = true;
if (row.length != value.length || row.length != length.length || value.length != length.length)
{
bool = false;
Tool.print("参数错误:插入表数据的时候列和值不对应!!!");
return false;
}
String sql1 = "insert into " + table + "(";
String sql2 = "";
for (int i = 0; i < row.length; i++)
{// 循环设置插入列
if (i != 0)
sql2 = sql2 + ",";
sql2 = sql2 + row[i];
}
String sql3 = ") values(";
String sql4 = "";
String sql5 = ")";
for (int i = 0; i < value.length; i++)// 循环设置值
{
value[i] = StringFormat.cleanInputText(value[i], length[i]);// 字长等于输入的长度
if (i != 0)
sql4 = sql4 + ",";
sql4 = sql4 + "?";
}
String consequence = sql1 + sql2 + sql3 + sql4 + sql5;
bool = this.runUpdate(consequence, value);
return bool;
}
/**
* 重写 have自动增长列 由自动增长放在row("第一个")第一个 已经处理危险字符 直接传入字符串即可 插入表数据
*
*
* String table = "car_info"; String[] row = new String[] { "c_id",
* "c_name", "c_MASS" }; String[] value = new String[] { "aaa.net", "sssss",
* "52" }; int [] length=new int[]{20,50,80};
*
* dao.insert(table, row, value,length);
*
* @param table
* 表名字
* @param row
* 待插入的列 into(c_no,c_name)
* @param value
* 待插入的值 "AAA" "VVV"
* @param length
* length 字符串长度
* @return boolean
*/
public boolean insertAuto(String table, String[] row, String[] value, int[] length)
{
boolean bool = true;
if (row.length != value.length || row.length != length.length || value.length != length.length)
{
bool = false;
Tool.print("参数错误:插入表数据的时候列和值不对应!!!");
return false;
}
String sql1 = "insert into " + table + "(";
String sql2 = "";
for (int i = 0; i < row.length; i++)
{// 循环设置插入列
if (i != 0)
sql2 = sql2 + ",";
sql2 = sql2 + row[i];
}
String sql3 = ") values(";
String sql4 = "";
String sql5 = ")";
for (int i = 0; i < value.length; i++)// 循环设置值
{
value[i] = StringFormat.cleanInputText(value[i], length[i]);// 字长等于输入的长度
if (i != 0)
sql4 = sql4 + ",";
sql4 = sql4 + "?";
}
String consequence = sql1 + sql2 + sql3 + sql4 + sql5;
consequence = consequence.replaceFirst("\\?", value[0]);
String newvalue[] = new String[(value.length - 1)];
for (int i = 1; i < value.length; i++)
{
newvalue[(i - 1)] = value[i];
}
// Tool.print(consequence);
bool = this.runUpdate(consequence, newvalue);
return bool;
}
/**
* 重写插入
*
* @param table
* 表名
* @param row
* 字段数组
* @param value
* 参数数组
* @return boolean
*/
public boolean insert(String table, String[] row, String value[])
{
boolean bool = true;
if (row.length != value.length)
{
bool = false;
Tool.print("参数错误:插入表数据的时候列和值不对应!!!");
return false;
}
String sql1 = "insert into " + table + "(";
String sql2 = "";
for (int i = 0; i < row.length; i++)
{// 循环设置插入列
if (i != 0)
sql2 = sql2 + ",";
sql2 = sql2 + row[i];
}
String sql3 = ") values(";
String sql4 = "";
String sql5 = ")";
for (int i = 0; i < value.length; i++)// 循环设置值
{
value[i] = StringFormat.cleanInputText(value[i], value[i].length());// 字长等于输入的长度
if (i != 0)
sql4 = sql4 + ",";
sql4 = sql4 + "?";
}
String consequence = sql1 + sql2 + sql3 + sql4 + sql5;
bool = this.runUpdate(consequence, value);
// Tool.print(consequence);
return bool;
}
/**
*
* 功能描述:检测一个表是否存在
* 访问类:BasicDAO 返回类型:boolean
*
* @param tableName
* @return boolean
*/
public boolean tableExist(String tableName)
{
// Tool.print("检测表" + tableName + "是否存在...");
String sql = "select tname from tab where tname =?";
boolean res = false;
String tName = getOneRs(sql, new String[] { tableName.toUpperCase() }, "tname");
if (!Tool.isNull(tName))
{
res = true;
// Tool.print("表" + tableName + "已存在");
}
else
{
Tool.print("表" + tableName + "不存在");
}
// Tool.print("检测表" + tableName + "是否存在完毕!");
return res;
}
/**
*
* 功能描述:检测表中的一个列
* 访问类:BasicDAO 返回类型:void
*
* @param tableName
* @param columName
* @param addColumSql
* @param addColumCommentSql
* @param index
*/
public void checkColumn(String tableName, String columName, String addColumSql, String addCommentSql, int index)
{
String sql = "select count(*) count from col where tname=? and cname=? ";
int lxidCount = Integer.parseInt(getOneRs(sql,
new String[] { tableName.toUpperCase(), columName.toUpperCase() }, "count"));
if (lxidCount == 0)
{
Tool.print("表" + tableName + "缺少列:" + columName + ",开始添加...");
runUpdate(addColumSql);
runUpdate(addCommentSql);
Tool.print("表" + tableName + "的列:" + columName + "添加完毕!");
}
}
/**
*
* 功能描述:检测表中的一个列
* 访问类:BasicDAO 返回类型:void
*
* @param tableName
* @param columName
* @param addColumSql
* @param addColumCommentSql
* @param index
*/
public void checkColumn(String tableName, String columName, String addColumSql, String addCommentSql)
{
String sql = "select count(*) count from col where tname=? and cname=? ";
int lxidCount = Integer.parseInt(getOneRs(sql,
new String[] { tableName.toUpperCase(), columName.toUpperCase() }, "count"));
if (lxidCount == 0)
{
Tool.print("表" + tableName + "缺少列:" + columName + ",开始添加...");
runUpdate(addColumSql);
runUpdate(addCommentSql);
Tool.print("表" + tableName + "的列:" + columName + "添加完毕!");
}
}
/**
*
* 功能描述:检测表中的一个列
* 访问类:BasicDAO 返回类型:void
*
* @param tableName
* @param columName
* @param addColumSql
* @param addColumCommentSql
* @param index
*/
public void checkColumn(String tableName, String columName, String addColumSql)
{
String sql = "select count(*) count from col where tname=? and cname=? ";
int lxidCount = Integer.parseInt(getOneRs(sql,
new String[] { tableName.toUpperCase(), columName.toUpperCase() }, "count"));
if (lxidCount == 0)
{
Tool.print("表" + tableName + "缺少列:" + columName + ",开始添加...");
if (!Tool.isNull(addColumSql))
{
runUpdate(addColumSql);
}
else
{
Tool.print("找不到列" + columName + "的添加语句!");
}
Tool.print("表" + tableName + "的列:" + columName + "添加完毕!");
}
}
/**
*
* 功能描述:创建表
* 访问类:BasicDAO 返回类型:void
*
* @param createTableSql
* @param addColumCommentSql
*/
public void createTable(String tableName, String createTableSql, String[] addCommentSql)
{
Tool.print("表" + tableName + "不存在,开始创建...");
runUpdate(createTableSql);
Tool.print("执行表" + tableName + "的创建完毕!");
Tool.print("开始添加表" + tableName + "的comment");
for (int i = 0; i < addCommentSql.length; i++)
{
runUpdate(addCommentSql[i]);
}
Tool.print("添加表" + tableName + "的comment完毕");
}
/**
*
* 功能描述:检测表中的所有列
* 访问类:BasicDAO 返回类型:void
*
* @param tableName
* @param column
* @param addColumSql
* @param addColumCommentSql
* @param index
*/
public void checkAllColumn(String tableName, String[] column, String[] addColumSql, String[] addCommentSql,
String[] primaryKey, String primarySql, int[] index)
{
Tool.print("开始检测" + tableName + "的列信息...");
for (int i = 0; i < column.length; i++)
{
checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), addColumSql[i], addCommentSql[i + 1]);
}
checkPrimaryKey(tableName.toUpperCase(), primaryKey, primarySql);
Tool.print("表" + tableName + "的列信息检测完毕!");
}
/**
*
* 功能描述:检测表中的所有列
* 访问类:BasicDAO 返回类型:void
*
* @param tableName
* @param column
* @param addColumSql
* @param addColumCommentSql
* @param index
*/
public void checkAllColumn(String tableName, String[] column, String[] addColumSql, String[] addCommentSql,
String[] primaryKey, String primarySql)
{
// Tool.print("开始检测" + tableName + "的列信息...");
if (addCommentSql.length == column.length + 1)
{
for (int i = 0; i < column.length; i++)
{
if (addColumSql.length > i && addCommentSql.length >= (i + 1))
{
checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), addColumSql[i], addCommentSql[i + 1]);
}
}
}
else
{
Tool.print(tableName + "的comment语句信息不正确,所以检测列信息而不执行comment 语句");
if (addColumSql != null && addColumSql.length == column.length)
{
for (int i = 0; i < column.length; i++)
{
checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), addColumSql[i]);
}
}
else
{
Tool.print("增加列的语句信息不正确,所以检测列信息而不执行增加列的语句");
for (int i = 0; i < column.length; i++)
{
checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), "");
}
}
}
checkPrimaryKey(tableName, primaryKey, primarySql);
// Tool.print("表" + tableName + "的列信息检测完毕!");
}
public void checkPrimaryKey(String tableName, String[] primaryKey, String primarySql)
{
// Tool.print("开始检测" + tableName + "的主键信息...");
String sql = "select col.column_name column_name from user_constraints con,user_cons_columns col where "
+ "con.constraint_name=col.constraint_name and con.constraint_type='P' and col.table_name=?";
List<HashMap<String, String>> list = getList(sql, new String[] { tableName.toUpperCase() },
new String[] { "column_name" });
String[] tempPrimary = Tool.listToArray(list, "column_name");
if (primaryKey == null || primaryKey.length == 0)
{
// Tool.print("表" + tableName + "被设计为没有主键!");
}
else
{
if (tempPrimary != null && (tempPrimary.length != primaryKey.length))
{
Tool.print("表" + tableName + "的主键信息不正确!下面开始修正...");
if (tempPrimary != null && tempPrimary.length > 0)
{
runUpdate("alter table " + tableName + " drop primary key ");
}
if (!Tool.isNull(primarySql))
{
Tool.print("给表" + tableName + "添加主键...");
runUpdate(primarySql);
Tool.print("表" + tableName + "添加主键完毕!");
}
// Tool.print("表" + tableName + "的主键修正完毕");
}
else
{
// Tool.print("表" + tableName + "的主键信息完整!");
}
}
// Tool.print("检测" + tableName + "的主键信息完毕");
}
/**
*
* 功能描述:判断一个实体是否存在例如触发器等
* 访问类:BasicDAO 返回类型:boolean
*
* @param objType
* @param objName
* @return boolean
*/
public boolean objExist(String objType, String objName)
{
boolean flag = false;
String sql = "select count(*)count from user_objects where object_type = ? and object_name = ?";
int count = Integer.parseInt(getOneRs(sql, new String[] { objType.toUpperCase(), objName.toUpperCase() },
"count"));
if (count > 0)
{
flag = true;
}
return flag;
}
public PreparedStatement getStmt()
{
return stmt;
}
public void setStmt(PreparedStatement stmt)
{
this.stmt = stmt;
}
}