package com.mwq.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; public class BaseDao { // 查询多个记录 protected Vector selectSomeNote(String sql) { Vector<Vector<Object>> vector = new Vector<Vector<Object>>();// 创建结果集向量 Connection conn = JDBC.getConnection();// 获得数据库连接 try { Statement stmt = conn.createStatement();// 创建连接状态对象 ResultSet rs = stmt.executeQuery(sql);// 执行SQL语句获得查询结果 int columnCount = rs.getMetaData().getColumnCount();// 获得查询数据表的列数 int row = 1;// 定义行序号 while (rs.next()) {// 遍历结果集 Vector<Object> rowV = new Vector<Object>();// 创建行向量 rowV.add(new Integer(row++));// 添加行序号 for (int column = 1; column <= columnCount; column++) { rowV.add(rs.getObject(column));// 添加列值 } vector.add(rowV);// 将行向量添加到结果集向量中 } rs.close();// 关闭结果集对象 stmt.close();// 关闭连接状态对象 } catch (SQLException e) { e.printStackTrace(); } return vector;// 返回结果集向量 } // 查询单个记录 protected Vector selectOnlyNote(String sql) { Vector<Object> vector = null; Connection conn = JDBC.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); int columnCount = rs.getMetaData().getColumnCount(); while (rs.next()) { vector = new Vector<Object>(); for (int column = 1; column <= columnCount; column++) { vector.add(rs.getObject(column)); } } rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); } return vector; } // 查询多个值 protected Vector selectSomeValue(String sql) { Vector<Object> vector = new Vector<Object>(); Connection conn = JDBC.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { vector.add(rs.getObject(1)); } rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); } return vector; } // 查询单个值 protected Object selectOnlyValue(String sql) { Object value = null; Connection conn = JDBC.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { value = rs.getObject(1); } rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); } return value; } // 插入、修改、删除记录 public boolean longHaul(String sql) { boolean isLongHaul = true;// 默认持久化成功 Connection conn = JDBC.getConnection();// 获得数据库连接 try { conn.setAutoCommit(false);// 设置为手动提交 Statement stmt = conn.createStatement();// 创建连接状态对象 stmt.executeUpdate(sql);// 执行SQL语句 stmt.close();// 关闭连接状态对象 conn.commit();// 提交持久化 } catch (SQLException e) { isLongHaul = false;// 持久化失败 try { conn.rollback();// 回滚 } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } return isLongHaul;// 返回持久化结果 } public static void main(String[] args) { BaseDao d=new BaseDao(); d.longHaul("delete from tb_order_item"); d.longHaul("delete from tb_order_form"); d.longHaul("delete from tb_user"); } }
最近下载更多
wanglinddad LV54
2023年12月18日
sunshine9920 LV12
2023年10月22日
E-dream LV1
2023年6月15日
微信网友_6511688260882432 LV3
2023年6月13日
WOWsuperman LV1
2023年6月6日
1135177454 LV1
2023年5月21日
UltimateI LV1
2023年5月19日
你们的代码都是我的了 LV16
2022年12月9日
飘逸人 LV4
2022年12月4日
ss106220 LV2
2022年5月30日
最近浏览更多
maoxp-1 LV2
4月14日
nineninenine
3月31日
暂无贡献等级
yanixh LV1
2月19日
mrsleep
1月18日
暂无贡献等级
ziv5466123 LV7
2023年12月19日
Henew168 LV2
2023年11月19日
fesfefe LV13
2023年11月6日
sunshine9920 LV12
2023年10月22日
zhaozhiwen123
2023年9月18日
暂无贡献等级
2036495585 LV9
2023年9月18日