DAO.java
资源名称:bangong.rar [点击查看]
上传用户:dlqqsh
上传日期:2021-11-13
资源大小:7840k
文件大小:19k
源码类别:
OA系统
开发平台:
Java
- package officeol.mc.tools;
- import java.io.*;
- import java.lang.reflect.*;
- import java.sql.*;
- import java.util.*;
- import javax.naming.*;
- import javax.sql.DataSource;
- public class DAO {
- public static String Log = "OFF";
- public static String username = "";
- public DAO() {
- super();
- }
- public static int executeUpdate(String sql) {
- int rowcount = 0;
- Connection con = null;
- Statement stmt = null;
- try {
- con = getConnection();
- stmt = con.createStatement();
- con.setAutoCommit(false);
- rowcount = stmt.executeUpdate(sql);
- con.commit();
- } catch (SQLException ex) {
- System.out.println("sql-----------" + sql);
- ex.printStackTrace();
- try {
- if (con != null) {
- con.rollback();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } catch (UnsupportedOperationException ex) {
- ex.printStackTrace();
- } finally {
- //释放资源
- if (stmt != null) {
- try {
- stmt.close();
- stmt = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return rowcount;
- }
- public static int executeUpdate(String sql, String s) {
- int rowcount = 0;
- Connection con = null;
- Statement stmt = null;
- try {
- con = getConnection();
- stmt = con.createStatement();
- rowcount = stmt.executeUpdate(sql);
- } catch (SQLException ex) {
- ex.printStackTrace();
- } catch (UnsupportedOperationException ex) {
- ex.printStackTrace();
- } finally {
- //释放资源
- if (stmt != null) {
- try {
- stmt.close();
- stmt = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return rowcount;
- }
- public static int executeProcedure(String procname, String para) {
- int rowcount = 0;
- Connection con = null;
- CallableStatement cstmt = null;
- try {
- con = getConnection();
- cstmt = con.prepareCall("{call " + procname + "(?)}");
- con.setAutoCommit(false);
- cstmt.setString(1, para);
- rowcount = cstmt.executeUpdate();
- con.commit();
- } catch (SQLException ex) {
- try {
- if (con != null) {
- con.rollback();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } catch (UnsupportedOperationException ex) {
- ex.printStackTrace();
- } finally {
- //释放资源
- if (cstmt != null) {
- try {
- cstmt.close();
- cstmt = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return rowcount;
- }
- /**
- * 创建一个数据库连接
- * 如果在应用服务器环境中配置了数据源,则从数据源中取. 否则自己创建.
- * @return 一个数据库连接对象
- */
- public static Connection getConnection() throws SQLException {
- Connection conn = null;
- /* String driver = Config.getDriver();
- String url = Config.getUrl();
- String username = Config.getUsername();
- String password = Config.getPassword();
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- conn = DriverManager.getConnection(url, username,
- password);*/
- try {
- System.out.println("开始读数据源");
- DataSource ds = null;
- Context initCtx = new InitialContext();
- Context ctx = (Context) initCtx.lookup("java:comp/env");
- //获取数据源对象
- Object obj = (Object) ctx.lookup("jdbc/train");
- ds = (javax.sql.DataSource) obj;
- conn=ds.getConnection();
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println("无法获得连接");
- }
- return conn;
- }
- public static Collection generalQuery(String sql, int first,
- int recNum,
- Class infoClass) {
- Connection con = null;
- Statement stmt = null;
- try {
- //获得连接,创建ResultSet
- con = getConnection();
- stmt = con.createStatement(ResultSet.
- TYPE_SCROLL_INSENSITIVE,
- ResultSet.
- CONCUR_READ_ONLY);
- ResultSet rs = stmt.executeQuery(sql);
- //找到第一条有效记录
- boolean hasRow = true;
- for (int i = 0; i < first && hasRow; i++) {
- hasRow = rs.next();
- }
- if (!hasRow && recNum != 0) { //结果集数据达不到first个数,此时取最后的几个
- //取最后一次的结果
- rs.last();
- int maxSize = rs.getRow();
- int begin = maxSize / recNum;
- int num = maxSize % recNum;
- first = begin * recNum;
- recNum = num;
- hasRow = true;
- }
- //要取得的个数是否超界
- rs.last();
- int maxSize = rs.getRow();
- rs.first();
- if (first + recNum > maxSize) {
- recNum = maxSize - first + 1;
- }
- //用来保存结果的类数组,就是将ResultSet结果赋给类数组
- ArrayList cs = new ArrayList();
- rs.first();
- if (first == 1) {} else {
- if (first != 0) {
- rs.absolute(first);
- }
- }
- Hashtable classFields = new Hashtable();
- List fieldList = new ArrayList();
- Field[] fields = null;
- Class superClass = infoClass;
- while (!superClass.getName().equalsIgnoreCase("java.lang.Object")) {
- fields = superClass.getDeclaredFields();
- for (int i = 0; i < fields.length; ++i) {
- fieldList.add(fields[i]);
- }
- superClass = superClass.getSuperclass();
- }
- fields = new Field[fieldList.size()];
- fieldList.toArray(fields);
- ResultSetMetaData rsmd = rs.getMetaData();
- //将ResultSet中的列的序号与类的属性对应起来
- int colCount = rsmd.getColumnCount();
- for (int i = 0; i < colCount; i++) {
- String colName = rsmd.getColumnName(i + 1); //列名
- for (int j = 0; j < fields.length; j++) {
- if (colName.toUpperCase().equals(fields[j].
- getName().
- toUpperCase())) {
- classFields.put(new Integer(i + 1),
- fields[j]);
- break;
- }
- }
- }
- //浏览ResultSet,将内容写入ArrayList
- if (recNum == 0) { //赋最大值
- recNum = maxSize;
- }
- for (int i = 0; i < recNum && hasRow; i++) {
- try {
- Object row = infoClass.newInstance();
- ArrayList colArray = new ArrayList();
- for (Enumeration enum1 = classFields.keys();
- enum1.hasMoreElements(); ) {
- colArray.add(enum1.nextElement());
- }
- Collections.sort(colArray);
- for (Iterator cols_it = colArray.iterator();
- cols_it.hasNext(); ) {
- Integer colIdx = (Integer) cols_it.next();
- Field theField = (Field) classFields.get(
- colIdx);
- Class paraType = theField.getType();
- theField.setAccessible(true);
- if (paraType.getName().equals("boolean")) {
- String temp = rs.getString(colIdx.
- intValue());
- boolean value = (temp == null ||
- temp.length() == 0 ||
- temp.charAt(0) ==
- '0') ? false : true;
- theField.setBoolean(row, value);
- } else if (paraType.getName().equals(
- "byte")) {
- theField.setByte(row,
- rs.getByte(colIdx.
- intValue()));
- } else if (paraType.getName().equals(
- "short")) {
- theField.setShort(row,
- rs.getShort(colIdx.
- intValue()));
- } else if (paraType.getName().equals("int")) {
- theField.setInt(row,
- rs.getInt(colIdx.
- intValue()));
- } else if (paraType.getName().equals(
- "long")) {
- theField.setLong(row,
- rs.getLong(colIdx.
- intValue()));
- } else if (paraType.getName().equals(
- "float")) {
- theField.setFloat(row,
- rs.getFloat(colIdx.
- intValue()));
- } else if (paraType.getName().equals(
- "double")) {
- theField.setDouble(row,
- rs.getDouble(colIdx.
- intValue()));
- } else if (paraType.getName().equals(
- "char")) {
- String temp = rs.getString(colIdx.
- intValue());
- char value = (temp == null ||
- temp.length() == 0) ?
- ' ' :
- temp.charAt(0);
- theField.setChar(row, value);
- } else if (paraType.getName().indexOf(
- "String") != -1) {
- String temp = rs.getString(colIdx.
- intValue());
- if (temp == null) {
- temp = "";
- }
- theField.set(row, temp);
- } else if (paraType.getName().indexOf(
- "Date") != -1) {
- java.sql.Date tempDate = rs.getDate(
- colIdx.intValue());
- theField.set(row, tempDate);
- }
- //处理byte[]
- else if (paraType.isArray() &&
- paraType.getComponentType().
- getName().equals("byte")) {
- byte[] tempBytes = rs.getBytes(
- colIdx.intValue());
- theField.set(row, tempBytes);
- } else {
- theField.set(row,
- rs.getObject(colIdx.
- intValue()));
- }
- }
- cs.add(row);
- } catch (Exception e) {
- //throw new RuntimeException(
- // "进行通用查询中的类型转换时出现问题:" + e.getMessage());
- e.printStackTrace();
- }
- //下一行
- hasRow = rs.next();
- }
- //返回结果
- return cs;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- //释放资源
- if (stmt != null) {
- try {
- stmt.close();
- stmt = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return null;
- }
- public static String[][] getArray(String sql) {
- ResultSet rs = null;
- Connection con = null;
- Statement stmt = null;
- if (sql != null) {
- try {
- con = getConnection();
- stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery(sql);
- ArrayList list = new ArrayList();
- if (rs != null) {
- while (rs.next()) {
- ArrayList columns = new ArrayList();
- int id = 1;
- while (true) {
- try {
- String temp = rs.getString(id);
- id++;
- columns.add(temp);
- } catch (Exception e) {
- list.add(columns);
- break;
- }
- }
- }
- }
- if (list != null && list.size() > 0) {
- String[][] temp = new String[list.size()][];
- ArrayList tempsub = null;
- for (int i = 0; i < list.size(); i++) {
- tempsub = (ArrayList) list.get(i);
- temp[i] = new String[tempsub.size()];
- for (int j = 0; j < tempsub.size(); j++) {
- temp[i][j] = (String) tempsub.get(j);
- }
- }
- return temp;
- }
- } catch (SQLException e) {
- if (con == null) {
- System.out.println("不能得到连接");
- }
- e.printStackTrace();
- } finally {
- if (rs != null) {
- try {
- rs.close();
- rs = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- stmt = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- return null;
- }
- public static DatabaseMetaData getMetaData() {
- Connection con = null;
- try {
- con = getConnection();
- if (con != null) {
- return con.getMetaData();
- } else {
- return null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- return null;
- }
- public static String[] getTableNames() {
- try {
- String[] types = new String[1];
- types[0] = "TABLE";
- ResultSet rs = getMetaData().getTables(null, null, null, types);
- ArrayList namelist = new ArrayList();
- if (rs != null) {
- while (rs.next()) {
- String tablename = rs.getString("TABLE_NAME");
- if (!tablename.equals("dtproperties")) {
- namelist.add(tablename);
- }
- }
- }
- if (!namelist.isEmpty()) {
- String[] names = new String[namelist.size()];
- for (int i = 0; i < namelist.size(); i++) {
- names[i] = (String) namelist.get(i);
- }
- return names;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String toGB(String iso) {
- try {
- return new String(iso.getBytes("ISO-8859-1"), "GB2312");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return iso;
- }
- }