图书管理系统(Java+Swing+mysql)
最近跟着网上视频试做了一个入门项目。该项目是一个简单入门项目,代码有点多不方便全部粘贴,不过会贴出重要代码部分,有需要源码的朋友可在文末获取。项目情况大致如下:
连接数据库登录、为了项目的体验更好加入前判空:
private void loginAction(ActionEvent evt) {
// TODO Auto-generated method stub
String username=this.userName.getText();
String password=new String(this.userPassword.getPassword());
if(StringUtil.isEmpty(username)) {
JOptionPane.showMessageDialog(null, "用户名不能为空");
return;
}
if(StringUtil.isEmpty(password)) {
JOptionPane.showMessageDialog(null, "密码不能为空");
return;
}
User user=new User(username,password);
Connection con=null;
try {
con=dbUtil.getCon();
User currentuser=userDao.login(con, user);
if(currentuser!=null) {
dispose();
new Main().setVisible(true);;
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
用户名是已存在、验证码框得到焦点时判断两次密码是否相同:
/**
* 用户名是否存在
* @param con
* @param name
* @return
* @throws Exception
*/
public boolean isExist(Connection con,String name) throws Exception {
String sql="select id from user where userName=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs=pstmt.executeQuery();
return rs.next();
}
/**
* 验证码框得到焦点时判断两次密码是否相同
*/
Verification1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Verification1.setText(StringUtil.RegsiterString());
}
对图书类别的:增、删(删除前判断类别下是否有图书)、改、查:
/**
* 添加
* @param con
* @param bookType
* @return
* @throws Exception
*/
public int add(Connection con, BookType bookType) throws Exception {
String sql = "insert into bookType values(null,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
return pstmt.executeUpdate();
}
/**
* 删除图书类别
* @param con
* @param id
* @return
* @throws Exception
*/
public int delete(Connection con,String id) throws Exception {
String sql="delete from booktype where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 判断类别下是否有图书
* @param con
* @param id
* @return
* @throws Exception
*/
public boolean exitBookByBookTypeId(Connection con,String id) throws Exception {
String sql="select * from book where bookTypeid=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,id);
ResultSet rs=pstmt.executeQuery();
return rs.next();
}
/**
* 更新数据类别
* @param con
* @param bookType
* @return
* @throws Exception
*/
public int updata(Connection con,BookType bookType) throws Exception {
String sql="update 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();
}
/**
* 查询图书类别
* @param con
* @param bookType
* @return
* @throws Exception
*/
public ResultSet list(Connection con,BookType bookType) throws Exception {
StringBuffer sb=new StringBuffer("select * from booktype");
if(StringUtil.isNotEmpty(bookType.getBookTypeName())) {
sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and","where"));
return pstmt.executeQuery();
}
对图书类别的:增、删、改、查:其原理与图书类别相似就不再重复贴。
页面布局使用了Swing插件相对较方便。
通过这个项目可以让自己对Mysql的增、删、改、查更熟悉,夯实基础很重要。
源码获取方式,关注菜鸟乐编程,然后回复:源码01图书管理系统