增删改查操作或方法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
查询的方法 public ClassBean getClassById(int id) { ClassBean bean = new ClassBean(); String sql = "select * from class where id=?"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); rs = pstmt.executeQuery(); if(rs.next()){ bean.setId(rs.getInt("id")); bean.setName(rs.getString("name")); } } catch (Exception e) { e.printStackTrace(); } finally{ try { rs.close(); pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return bean; }
if (name != null && !"".equals(name) && name.length() > 2) { ClassBean bean = new ClassBean(); bean.setId(id); bean.setName(name); if (dao.updateClass(bean)) { JOptionPane.showMessageDialog(this, "操作成功"); } else { JOptionPane.showMessageDialog(this, "操作失败"); } this.fillTable(); } }
} else { JOptionPane.showMessageDialog(this, "操作失败"); } this.fillTable(); } } //修改的方法 public boolean updateClass(ClassBean bean) { boolean flog = false; String sql = "update class set name=? where id=?"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setString(1, bean.getName()); pstmt.setInt(2, bean.getId()); if(pstmt.executeUpdate()>0){ flog = true; } } catch (Exception e) { e.printStackTrace(); } finally{ try { pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return flog; }
selectAll(); }
public void bntadd_actionPerformed(ActionEvent e) { clearTableData(); Student s=new Student();
s.setName(txtname.getText()); s.setSex(Man.isSelected()?"男":"女"); int age=Integer.parseInt(txtage.getText()); s.setAge(age); sb.addStudent(s);
查询全部的方法 public List<ClassBean> findAll() { List<ClassBean> beans = new ArrayList<ClassBean>(); String sql = "select * from class"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ ClassBean bean = new ClassBean(); bean.setId(rs.getInt("id")); bean.setName(rs.getString("name")); beans.add(bean); } } catch (Exception e) { e.printStackTrace(); } finally{ try { rs.close(); pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return beans; }
增删来自百度文库查操作或方法
//鼠标选中某一行后,显示在文本框中; private void jTable1MouseClicked(java.awt.event.MouseEvent evt) { int row = this.jTable1.getSelectedRow(); Integer id = (Integer) this.jTable1.getValueAt(row, 0); String name = (String) this.jTable1.getValueAt(row, 1); this.txtId.setText(id.toString()); this.txtName.setText(name); } //删除的方法 public boolean removeClass(int id) { boolean flog = false; String sql = "delete from class where id=?"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); if(pstmt.executeUpdate()>0){ flog = true; } } catch (Exception e) { e.printStackTrace(); } finally{ try { pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return flog; }
将全部数据显示在 Table 中 public void fillTable() { List<ClassBean> beans = dao.findAll(); DefaultTableModel dtm = (DefaultTableModel) this.jTable1.getModel(); dtm.setRowCount(0); for (ClassBean bean : beans) { Vector v = new Vector(); v.add(bean.getcId()); v.add(bean.getcName()); dtm.addRow(v); } } 单选按钮的使用 简化如下
int id = Integer.parseInt(sid); if (dao.removeClass(id)) { JOptionPane.showMessageDialog(this, "操作成功"); } else { JOptionPane.showMessageDialog(this, "操作失败"); } this.fillTable(); } } 添加的方法 public boolean addClass(ClassBean bean) { boolean flog = false; String sql = "insert into class(name) values(?)"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setString(1, bean.getName()); if(pstmt.executeUpdate()>0){ flog = true; } } catch (Exception e) { e.printStackTrace(); } finally{ try { pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return flog; } //添加操作
//修改操作 private void btnEditActionPerformed(java.awt.ev ent.ActionEvent evt) { String name = this.
txtName.getText(); String sid = this.txtId.getText(); if (sid != null && !sid.equals("")) { int id = Integer.parseInt(sid);
//删除操作 private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) { String sid = this.txtId.getText(); if (sid != null && !sid.equals("")) {
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { String name = this.txtName.getText(); if (name != null && !"".equals(name) && name.length() > 2) { ClassBean bean = new ClassBean(); bean.setName(name); if (dao.addClass(bean)) { JOptionPane.showMessageDialog(this, "操作成功");
查询的方法 public ClassBean getClassById(int id) { ClassBean bean = new ClassBean(); String sql = "select * from class where id=?"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); rs = pstmt.executeQuery(); if(rs.next()){ bean.setId(rs.getInt("id")); bean.setName(rs.getString("name")); } } catch (Exception e) { e.printStackTrace(); } finally{ try { rs.close(); pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return bean; }
if (name != null && !"".equals(name) && name.length() > 2) { ClassBean bean = new ClassBean(); bean.setId(id); bean.setName(name); if (dao.updateClass(bean)) { JOptionPane.showMessageDialog(this, "操作成功"); } else { JOptionPane.showMessageDialog(this, "操作失败"); } this.fillTable(); } }
} else { JOptionPane.showMessageDialog(this, "操作失败"); } this.fillTable(); } } //修改的方法 public boolean updateClass(ClassBean bean) { boolean flog = false; String sql = "update class set name=? where id=?"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setString(1, bean.getName()); pstmt.setInt(2, bean.getId()); if(pstmt.executeUpdate()>0){ flog = true; } } catch (Exception e) { e.printStackTrace(); } finally{ try { pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return flog; }
selectAll(); }
public void bntadd_actionPerformed(ActionEvent e) { clearTableData(); Student s=new Student();
s.setName(txtname.getText()); s.setSex(Man.isSelected()?"男":"女"); int age=Integer.parseInt(txtage.getText()); s.setAge(age); sb.addStudent(s);
查询全部的方法 public List<ClassBean> findAll() { List<ClassBean> beans = new ArrayList<ClassBean>(); String sql = "select * from class"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ ClassBean bean = new ClassBean(); bean.setId(rs.getInt("id")); bean.setName(rs.getString("name")); beans.add(bean); } } catch (Exception e) { e.printStackTrace(); } finally{ try { rs.close(); pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return beans; }
增删来自百度文库查操作或方法
//鼠标选中某一行后,显示在文本框中; private void jTable1MouseClicked(java.awt.event.MouseEvent evt) { int row = this.jTable1.getSelectedRow(); Integer id = (Integer) this.jTable1.getValueAt(row, 0); String name = (String) this.jTable1.getValueAt(row, 1); this.txtId.setText(id.toString()); this.txtName.setText(name); } //删除的方法 public boolean removeClass(int id) { boolean flog = false; String sql = "delete from class where id=?"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); if(pstmt.executeUpdate()>0){ flog = true; } } catch (Exception e) { e.printStackTrace(); } finally{ try { pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return flog; }
将全部数据显示在 Table 中 public void fillTable() { List<ClassBean> beans = dao.findAll(); DefaultTableModel dtm = (DefaultTableModel) this.jTable1.getModel(); dtm.setRowCount(0); for (ClassBean bean : beans) { Vector v = new Vector(); v.add(bean.getcId()); v.add(bean.getcName()); dtm.addRow(v); } } 单选按钮的使用 简化如下
int id = Integer.parseInt(sid); if (dao.removeClass(id)) { JOptionPane.showMessageDialog(this, "操作成功"); } else { JOptionPane.showMessageDialog(this, "操作失败"); } this.fillTable(); } } 添加的方法 public boolean addClass(ClassBean bean) { boolean flog = false; String sql = "insert into class(name) values(?)"; try { if(conn==null || conn.isClosed()){ conn = DBUtil.getConnection(); } pstmt = conn.prepareStatement(sql); pstmt.setString(1, bean.getName()); if(pstmt.executeUpdate()>0){ flog = true; } } catch (Exception e) { e.printStackTrace(); } finally{ try { pstmt.close(); conn.close(); } catch (Exception e2) { e2.printStackTrace(); } } return flog; } //添加操作
//修改操作 private void btnEditActionPerformed(java.awt.ev ent.ActionEvent evt) { String name = this.
txtName.getText(); String sid = this.txtId.getText(); if (sid != null && !sid.equals("")) { int id = Integer.parseInt(sid);
//删除操作 private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) { String sid = this.txtId.getText(); if (sid != null && !sid.equals("")) {
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { String name = this.txtName.getText(); if (name != null && !"".equals(name) && name.length() > 2) { ClassBean bean = new ClassBean(); bean.setName(name); if (dao.addClass(bean)) { JOptionPane.showMessageDialog(this, "操作成功");