您好,欢迎光临本网站![请登录][注册会员]  
文件名称: servlet制作网站
  所属分类: Java
  开发工具:
  文件大小: 17mb
  下载次数: 0
  上传时间: 2013-04-28
  提 供 者: u0100*****
 详细说明: package com.xr.util; import java.lang.reflect.Method; import java.sql.*; import java.util.*; import org.junit.Test; import com.xr.dto.ManagerDTO; public class DBHelper { private Connection conn = null; { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("创建驱动失败"); e.printStackTrace(); } try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/xc","root", "root"); } catch (SQLExcepti on e) { System.out.println("创建连接失败"); e.printStackTrace(); } } // 单表新增,修改,删除 public boolean excuteUpdate(String sql) { if (conn == null) return false; boolean sign = false; try { Statement stat = conn.createStatement(); int i = stat.executeUpdate(sql); if (i > 0) { sign = true; } else { sign = false; } } catch (Exception ex) { ex.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { } return sign; } } // 多表新增,修改,删除 public boolean excuteUpdate(Object... sql) { if (conn == null) return false; boolean sign = false; try { conn.setAutoCommit(false); Statement stat = conn.createStatement(); for (Object o : sql) { stat.addBatch(o.toString()); } stat.executeBatch(); conn.commit(); sign = true; } catch (Exception ex) { conn.rollback(); sign = false; ex.printStackTrace(); } finally { try { conn.setAutoCommit(true); conn.close(); } catch (SQLException e) { } return sign; } } @Test public void tet() throws SQLException{ List list=(List) conn.createStatement().executeQuery("select name from tbl_manager where B_id='1'"); ManagerDTO m=(ManagerDTO) list.get(0); System.out.println("aaaaaaa"+m.getName()); } // 单表查询 public List excuteQuery(String sql, String objName) { if (conn == null) return null; List list = new ArrayList(); try { Statement stat = conn.createStatement(); ResultSet re = stat.executeQuery(sql); Class c = Class.forName(objName); Method[] ms = c.getDeclaredMethods(); ResultSetMetaData rsmd=re.getMetaData(); while (re.next()) { Object obj = c.newInstance(); for (int i = 0; i < ms.length; i++) { Method m = ms[i]; if (m.getName().startsWith("set")) { String name = m.getName().substring(3); for(int j=1;j<=rsmd.getColumnCount();j++){ if(name.equalsIgnoreCase(rsmd.getColumnName(j))){ m.invoke(obj, re.getString(name)==null?"":re.getString(name)); break; } } } } list.add(obj); } } catch (Exception ex) { list = null; ex.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { } return list; } } /*标量结果*/ public Object executeScare(String sql){ if (conn == null) return null; Object obj = null; try { Statement stat = conn.createStatement(); ResultSet re = stat.executeQuery(sql); if (re.next()) { obj=re.getString(1); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { } return obj; } } // 单个对象的查询 public Object excuteOneQuery(String sql, String objName) { if (conn == null) return null; Object obj = null; try { Statement stat = conn.createStatement(); ResultSet re = stat.executeQuery(sql); Class c = Class.forName(objName); Method[] ms = c.getDeclaredMethods(); ResultSetMetaData rsmd = re.getMetaData(); if (re.next()) { obj = c.newInstance(); for (int i = 0; i < ms.length; i++) { Method m = ms[i]; if (m.getName().startsWith("set")) { String name = m.getName().substring(3); for(int j=1;j<=rsmd.getColumnCount();j++){ if(name.equalsIgnoreCase(rsmd.getColumnName(j))){ m.invoke(obj, re.getString(name)==null?"":re.getString(name)); break; } } } } } } catch (Exception ex) { ex.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { } return obj; } } // 多表查询 public List excuteQuery(String sql) { if (conn == null) return null; List list = new ArrayList(); try { Statement stat = conn.createStatement(); ResultSet re = stat.executeQuery(sql); ResultSetMetaData remd = re.getMetaData(); while (re.next()) { List row = new ArrayList(); for (int i = 1; i < remd.getColumnCount() + 1; i++) { row.add(re.getString(i)==null?"":re.getString(i)); } list.add(row); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { } return list; } } // 多表查询,单条记录返回 public List excuteOneQuery(String sql) { if (conn == null) return null; List row = new ArrayList(); try { Statement stat = conn.createStatement(); ResultSet re = stat.executeQuery(sql); ResultSetMetaData remd = re.getMetaData(); if (re.next()) { for (int i = 1; i < remd.getColumnCount() + 1; i++) { row.add(re.getString(i)); } } } catch (Exception ex) { ex.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { } return row; } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: servlet
 输入关键字,在本站1000多万海量源码库中尽情搜索: