因为这几个功能的实现不难,所有就Po个图,还有函数的实现,以及Button的监听事件处理。图书类别、图书都会实现这增删改查功能所以就放其中一个的吧。
private int id;
private String bookTypeName;
private String bookTypeDesc;
/**
* 字符串工具类
* @author Amos0602
*
*/
public class StringUtil {
public static boolean isEmpty(String string) {
if (string==null||"".equals(string.trim())) {
return true;
}else {
return false;
}
}
public static boolean isNotEmpty(String string) {
if (string!=null && !"".equals(string.trim())) {
return true;
} else {
return false;
}
}
}
/**
* 图书类别Dao类
* @author Amos0602
*
*/
public class BookTypeDao {
/**
* 图书类别增加
* @param connection
* @param bookType
* @return
* @throws Exception
*/
public int add(Connection connection,BookType bookType)throws Exception {
String sql="insert into t_bookType values(null,?,?)";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1, bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
return pstmt.executeUpdate();
}
/**
* 删除图书类别
* @param connection
* @param id
* @return
* @throws Exception
*/
public int delete(Connection connection,String id)throws Exception {
String sql="delete from t_booktype where id=?";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 更新图书类别
* @param connection
* @param bookType
* @return
* @throws Exception
*/
public int update(Connection connection,BookType bookType)throws Exception{
String sql="update t_booktype set bookTypeName=?,bookTypeDesc=? where id=?";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1, bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
pstmt.setInt(3, bookType.getId());
return pstmt.executeUpdate();
}
/**
* 查询图书类别集合
* @param connection
* @param bookType
* @return
* @throws Exception
*/
public ResultSet list(Connection connection,BookType bookType) throws Exception{
StringBuffer sBuffer=new StringBuffer("select * from t_booktype");
if (StringUtil.isNotEmpty(bookType.getBookTypeName())) {
sBuffer.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
}
PreparedStatement pstmt=connection.prepareStatement(sBuffer.toString().replaceFirst("and", "where"));
return pstmt.executeQuery();
}
}
/**
* 图书类型添加事件
* @param e
*/
private void bookTypeAddActionPerformed(ActionEvent e) {
String bookTypeName=this.bookTypeNameTxt.getText();
String bookTypeDesc=this.bookTypeDescTxt.getText();
if (StringUtil.isEmpty(bookTypeName)) {
JOptionPane.showMessageDialog(null, "图书类别名称不能为空!");
return;
}
BookType bookType=new BookType(bookTypeName,bookTypeDesc);
Connection connection=null;
try {
connection=dbUtil.getConnection();
int n=bookTypeDao.add(connection, bookType);
if (n==1) {
JOptionPane.showMessageDialog(null, "图书类别添加成功!");
resetValue();
}else {
JOptionPane.showMessageDialog(null, "图书类别添加失败!");
}
} catch (Exception e2) {
e2.printStackTrace();
JOptionPane.showMessageDialog(null, "图书类别添加失败!");
}finally {
try {
dbUtil.closeConnection(connection);
} catch (Exception e3) {
}
}
}
/**
* 图书类别删除事件处理
* @param arg0
*/
private void bookTypeDeleteActionEvent(ActionEvent arg0) {
String id=idTxt.getText();
if (StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗?");
if (n==0) {
Connection connection=null;
try {
connection=dbUtil.getConnection();
int deleteNum=bookTypeDao.delete(connection, id);
if (deleteNum==1) {
JOptionPane.showMessageDialog(null, "删除成功!");
this.resetValue();
this.fillTable(new BookType());
}else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
}finally {
try {
dbUtil.closeConnection(connection);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
/**
* 修改图书类别事件处理
* @param arg0
*/
private void bookTypeUpdateActionEvent(ActionEvent arg0) {
String id=idTxt.getText();
String bookTypeName=bookTypeNameTxt.getText();
String bookTypeDesc=bookTypeDescTxt.getText();
if (StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要修改的记录");
return;
}
if (StringUtil.isEmpty(bookTypeName)) {
JOptionPane.showMessageDialog(null, "图书类别名称不能为空");
return;
}
BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);
Connection connection=null;
try {
connection=dbUtil.getConnection();
int modifyNum=bookTypeDao.update(connection, bookType);
if (modifyNum==1) {
JOptionPane.showMessageDialog(null, "修改成功!");
this.resetValue();
this.fillTable(new BookType());
}else {
JOptionPane.showMessageDialog(null, "修改失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
dbUtil.closeConnection(connection);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
/**
* 图书类别查询事件处理
* @param arg0
*/
private void bookTypeSeachActionPerFormed(ActionEvent evt) {
String s_bookTypeName=this.s_bookTypeNameTxt.getText();
BookType bookType=new BookType();
bookType.setBookTypeName(s_bookTypeName);
this.fillTable(bookType);
}
/**
* 表格行点击事件处理
* @param arg0
*/
private void bookTypeTableMousePressed(MouseEvent arg0) {
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));
}
重置表单封装了一下,后面方便使用
/**
* 重置事件处理
* @param arg0
*/
private void resetValueActionPerformed(ActionEvent arg0) {
this.resetValue();//已封装,往下
}
/*
* 重置表单
*/
private void resetValue() {
this.bookTypeDescTxt.setText("");
this.bookTypeNameTxt.setText("");
}
后面的图书的管理,跟图书类别管理一样的增删改查…