实现图书类别修改功能
1、添加修改的Dao方法
/**
* 图书类别Dao类
* @author Administrator
*
*/
public class BookTypeDao {
/**
* 图书类别添加
* @param con
* @param bookType
* @return
* @throws Exception
*/
public static int add(Connection con,BookType bookType)throws Exception{
String sql="insert into t_bookType values(null,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
return pstmt.executeUpdate();//执行
}
/**
* 查询图书类别集合
* @param con
* @param bookType
* @return
* @throws Exception
*/
public ResultSet list(Connection con,BookType bookType)throws Exception{
StringBuffer sb=new StringBuffer("select * from t_bookType");//条件是动态的,用拼接的方式
//如果不为空,动态添加
if(StringUtil.isNotEmpty(bookType.getBookTypeName())){
sb.append(" and bookTypeName like'%"+bookType.getBookTypeName()+"%'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));//先转换成字符串,调用字符串方法把and换位where
return pstmt.executeQuery();
}
-----------------------------------------新添加----------------------------------
/**
* 删除图书类别
* @param con
* @param id
* @return
* @throws Exception
*/
public int delete(Connection con,String id)throws Exception{
String sql="delete from t_bookType where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 修改图书类别
* @param con
* @param bookType
* @return
* @throws Exception
*/
public int update(Connection con,BookType bookType)throws Exception{
String sql="update t_bookType set bookTypeName=?,bookTypeDesc=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
pstmt.setInt(3, bookType.getId());
return pstmt.executeUpdate();
}
---------------------------------------------------------------------------------
}
2、在BookTypeManageInterFrm添加修改界面
添加JPanel组件:修改border的属性,改为TitledBorder。修改布局为GroupLayout
在JPanel组件里添加JLabel,JTextField,JButton,JTextArea组件
把显示id的JTextField设置为不可修改,重命名为idTxt
把显示图书类别名称的JTextField重命名为bookTypeNameTxt
把显示描述的JTextField重命名为bookTypeDescTxt
给描述的JTextField添加边框
//设置文本域边框
bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
3、实现点击图书类别,表单显示类别的详细信息的功能
给bookTypeTable添加鼠标点击事件
@Override
public void mousePressed(MouseEvent e) {
bookTypeTableMousePressed(e);
}
/**
* 表格行点击事件处理
* @param e
*/
private void bookTypeTableMousePressed(MouseEvent evt) {
int row=bookTypeTable.getSelectedRow();//获取选中行,返回行号
idTxt.setText((String)bookTypeTable.getValueAt(row, 0));//获取第几行,第几列的值
bookTypeNameTxt.setText((String)bookTypeTable.getValueAt(row, 1));
bookTypeDescTxt.setText((String)bookTypeTable.getValueAt(row, 2));
}
4、实现修改按钮功能
修改按钮添加事件
public void actionPerformed(ActionEvent e) {
bookTypeUpdateActionEvent(e);
}
封装一个方法,用来重置数据
/**
* 重置表单
*/
private void resetValue(){
this.idTxt.setText("");
this.bookTypeNameTxt.setText("");
this.bookTypeDescTxt.setText("");
}
/**
* 图书类别修改事件处理
* @param evt
*/
private void bookTypeUpdateActionEvent(ActionEvent evt) {
//获取值
String id=idTxt.getText();
String bookTypeName=bookTypeNameTxt.getText();
String bookTypeDesc=bookTypeDescTxt.getText();
//判断id,用户什么都没点,不可修改
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null, "请选择一个图书类!");
}
BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);
Connection con=null;
try{
con=dbUtil.getCon();
int modifyNum=bookTypeDao.update(con, bookType);
if(modifyNum==1){
JOptionPane.showMessageDialog(null, "修改成功");
this.resetValue();//修改成功后,重置数据
this.fillTable(new BookType());//刷新表格table的数据
}else{
JOptionPane.showMessageDialog(null, "修改失败");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.close(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5、实现图书类别删除功能
新建一个删除按钮事件
public void actionPerformed(ActionEvent e) {
bookTypeDeleteActionEvent(e);
}
/**
* 图书类别删除事件处理
* @param e
*/
private void bookTypeDeleteActionEvent(ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null, "请选择要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null, "是否删除该记录");
if(n==0){
Connection con=null;
try{
con=dbUtil.getCon();
int deleteNum=bookTypeDao.delete(con, id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null, "删除成功");
this.resetValue();
this.fillTable(new BookType());
}else{
JOptionPane.showMessageDialog(null, "删除失败");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.close(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}