dbdao3

 /**
  * 執行SQL更新語句,針對Statement﹐可以用于事物處理
  *
  * @param sql
  *            String 執行的SQL語句
  * @return int 執行成功的SQL條數
  */
 public int executeUpdate(String sql) throws SQLException {
  int num = 0;
  Statement statement = null;
  try {
   statement = createStatement();
   num = statement.executeUpdate(sql);
   if (isAutoCommit) {
    connection.commit();
   }
  } catch (SQLException e) {
   if (isAutoCommit) {
    connection.rollback();
   }
   LOG.error(e.toString(), e);
   throw e;
  } finally {
   if (statement != null) {
    statement.close();
    statement = null;
   }
  }
  return num;
 }

 /**
  * 執行資料更新操作,接收參數﹐可以用于執行多條操作的事物處理﹐可以回滾
  *
  * @param sql-String
  *            增加,修改和刪除SQL語句,注意語句要帶?參數 形如:insert into tbl_group values(?,?,?)
  * @param args []
  *            Object參數物件陣列, 形如: <p/> Object obj []= new Object[3]; <br/>
  *            obj[0] = new String("string1"); <br/> obj[1] = new
  *            String("string2");<br/> obj[2] = new Integer(1);<br/>
  * @return int 執行成功返回受影響的記錄數
  */
 public int executeUpdate(String sql, Object[] args) throws SQLException {
  int r = 0;
  PreparedStatement preparedStatement = null;
  try {
   preparedStatement = connection.prepareStatement(sql);
   if (args != null) {
    for (int i = 0; i < args.length; i++) {
     preparedStatement.setObject(i + 1, args[i]);
    }
   }
   
   r = preparedStatement.executeUpdate();
   if (isAutoCommit) {
    connection.commit();
   }
  } catch (SQLException e) {
   if (isAutoCommit) {
    connection.rollback();
   }
   LOG.error(e.toString(), e);
   throw e;
  } finally {
   if (preparedStatement != null) {
    preparedStatement.close();
    preparedStatement = null;
   }
  }
  return r;
 }

 /**
  *
  * 執行多條SQL語句,用半形分號分隔﹐可以設置isAutoCommit的屬性﹐然后commit(),rollback() 使用方法:<br>
  * DBDAO dao = new DBDAO(); //或者直接調用帶參數的構造方法﹐不再設置isAutoCommit的屬性
  * dao.setAutoCommit(false); int[] count = dao.doBatch(sql); dao.commit();
  * //或者dao.rollback(); dao.close();
  *
  * @param sql
  *            String 形如:"select * from tlb_a;delete from tlb_b"
  * @return int[] 對應分號相隔的sql語句執行成功的條數
  * @throws SQLException
  *             SQL異常
  */

 public int[] doBatch(String sql) throws SQLException {
  int[] rowResult = null;
  String a;
  Statement statement = null;
  try {
   statement = connection.createStatement();
   java.util.StringTokenizer st = new java.util.StringTokenizer(sql, ";");
   while (st.hasMoreElements()) {
    a = st.nextToken();
    statement.addBatch(a);
   }
   rowResult = statement.executeBatch();
  } catch (SQLException e) {
   LOG.error(e.toString(), e);
   throw e;
  } finally {
   if (statement != null) {
    statement.close();
    statement = null;
   }
  }
  ArrayList al=new ArrayList();
  ArrayList bl=new ArrayList(al);
  return rowResult;
 }

 /**
  * 新增批處理方法 支持SQL預編譯 ,但一次只能處理一個SQL.<br>
  * <b> 注意:<br>
  * <ul>
  * <li>為避免用戶漏置 autocommit參數,出現非預期的結果,本方法內置 autoCommit=false
  * <li>方法內未調用connection.commit(),connection.close()方法,需用戶自行調用.
  * <li>方法內置 connection.rollback(),用戶只需捕獲異常即可.</b>
  * </ul>
  * 例如:
  *
  * <pre>
  * DBDAO db = new DBDAO();
  * String sql = &quot;update table1 set column1=?, column2=? where id=?&quot;;
  * Object[][] param = { { &quot;value11&quot;, &quot;value12&quot;, 1 }, { &quot;value21&quot;, &quot;value22&quot;, 2 }, { &quot;value31&quot;, &quot;value32&quot;, 3 } };
  * db.doBatch(sql, param);
  * </pre>
  *
  * @param sql
  * @param param
  * @return
  * @throws SQLException
  */
 public int[] doBatch(String sql, Object[][] param) throws SQLException {
  int[] rowResult = null;
  PreparedStatement pstmt = null;
  try {
   connection.setAutoCommit(false);
   pstmt = connection.prepareStatement(sql);
   for (int i = 0; i < param.length; i++) {
    for (int j = 0; j < param[i].length; j++)
     pstmt.setObject(j + 1, param[i][j]);
    pstmt.addBatch();
   }
   rowResult = pstmt.executeBatch();
  } catch (SQLException e) {
   connection.rollback();
   LOG.error(e.toString(), e);
   throw e;
  } finally {
   if (pstmt != null) {
    pstmt.close();
    pstmt = null;
   }
  }
  return rowResult;
 }

 /**
  * close statement object
  *
  * @throws SQLException
  */
 public void closeStatement() throws SQLException {
  if (statement != null) {
   statement.close();
   statement = null;
  }
 }

 /**
  * close preparedstatement object
  *
  * @throws SQLException
  */
 public void closePreparedStatement() throws SQLException {
  if (preparedStatement != null) {
   preparedStatement.close();
   preparedStatement = null;
  }
 }

 /**
  * 關閉連接。 <p/>
  *
  * @throws SQLException
  *             SQL異常
  */
 public void close() throws SQLException {
  try {
   if (statement != null) {
    statement.close();
    statement = null;
   }
   if (preparedStatement != null) {
    preparedStatement.close();
    preparedStatement = null;
   }
   if (resultset != null) {
    resultset.close();
    resultset = null;
   }
   if (connection != null) {
    connection.close();
    connection = null;
   }
  } catch (SQLException e) {
   LOG.error(e.toString(), e);
   throw e;
  }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JFrame jf = new JFrame("功能界面"); jf.setBounds(0, 0, 700, 600); jf.setVisible(true); jf.setLayout(null); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b1 = new JButton("图书入库"); b1.setBounds(20, 90, 150, 80); JButton b2 = new JButton("图书查询"); b2.setBounds(20, 210, 150, 80); JButton b3 = new JButton("图书修改"); b3.setBounds(500, 90, 150, 80); JButton b5 = new JButton("办理借阅证登记"); b5.setBounds(20, 330, 150, 80); JButton b6 = new JButton("图书借阅管理"); b6.setBounds(500, 210, 150, 80); JButton b4 = new JButton("图书删除"); b4.setBounds(500, 330, 150, 80); JButton b7 = new JButton("退出系统"); b7.setBounds(560, 20, 90, 30); ta.setBounds(190, 90, 290, 320); txt.setBounds(120,450,300,30); JButton b8 = new JButton("确定"); b8.setBounds(440,450,70,30); JMenuBar menuBar = new JMenuBar(); JMenu menuManage = new JMenu("图书管理"); JMenu menuAbout = new JMenu("关于系统"); JMenuItem item1 = new JMenuItem("图书列表"); menuManage.add(item1); item1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame("图书列表"); frame.setSize(600, 400); frame.setLocationRelativeTo(null); ListPanel listPanel = new ListPanel(); frame.add(listPanel); frame.setVisible(true); } }); ActionListener act = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource().equals(b1)) { insertNewbook(); }else if(e.getSource().equals(b3)) { updatebook(); }else if(e.getSource().equals(b4)) { String bid = txt.getText(); if(bid!=null) { if(dbDao.delelteBook(bid)){ int user = JOptionPane.showConfirmDialog(null, "图书删除成功","提示",JOptionPane.YES_NO_OPTION); }else{ int user = JOptionPane.showConfirmDialog(null, "图书删除失败","提示",JOptionPane.YES_NO_OPTION); } }else { return; }部分代码,点击图书删除按钮会直接弹窗提示删除失败并且bid是空值,该怎么获取输入的值并将它传入删除图书方法
05-27

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值