成品图
项目环境:eclipse +java1.8.0.121+mysql8
主要使用MVC结构
项目结构截图
model封装图书类别和图书的实体类
dao封装操作数据库 和用户登录的操作类
view封装每一个界面的类
util封装数据库连接操作
包结构截图
贴一下 主要操作数据库的Dao类操作 方便以后查看
图书Dao类
package com.java1234.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.java1234.model.Book;
import com.java1234.model.BookType;
import com.java1234.util.StringUtil;
/**
* 图书Dao类
* @author Administrator
*
*/
public class BookDao {
public int addBook(Connection conn,Book book)throws Exception {
String sql="insert into t_book values(null,?,?,?,?,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,book.getBookName() );
pstmt.setString(2,book.getAuthor() );
pstmt.setString(3,book.getSex() );
pstmt.setFloat(4,book.getPrice());
pstmt.setInt(5,book.getBookTypeId());
pstmt.setString(6,book.getBookDesc());
return pstmt.executeUpdate();
}
/**
* 查询图书
* @param conn
* @param book
* @return
*/
public ResultSet list(Connection conn,Book book) throws Exception{
StringBuffer sb=new StringBuffer("select * from t_book b, t_bookType bt where b.bookTypeId=bt.id");
if(StringUtil.isNotEmpty(book.getBookName())) {
sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
}
if(StringUtil.isNotEmpty(book.getAuthor())) {
sb.append(" and b.author like'%"+book.getAuthor()+"%'");
}
if(book.getBookTypeId()!=null && book.getBookTypeId()!=-1) {
sb.append("and b.bookTypeId="+book.getBookTypeId());
}
System.out.println("sql:"+sb.toString());
PreparedStatement pstmt=conn.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
/**
* 删除记录
* @param conn
* @param book
* @return
*/
public int delete(Connection conn,String id)throws Exception {
String sql="delete from t_book where id=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 更新图书信息
* @param conn
* @param bookType
* @return
* @throws Exception
*/
public int update(Connection conn,Book book) throws Exception{
String sql="update t_book set bookName=?,sex=?,price=?,author=?,bookTypeId=?,bookDesc=? where id=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,book.getBookName());
pstmt.setString(2, book.getSex());
pstmt.setFloat(3, book.getPrice());
pstmt.setString(4, book.getAuthor());
pstmt.setInt(5, book.getBookTypeId());
pstmt.setString(6, book.getBookDesc());
pstmt.setInt(7, book.getId());
return pstmt.executeUpdate();
}
}
图书类别Dao
package com.java1234.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.java1234.model.BookType;
import com.java1234.util.StringUtil;
/**
* 图书类别Dao类
* @author Administrator
*
*/
public class BookTypeDao {
/**
* 图书类别添加
* @param conn
* @param booktype
* @return
* @throws Exception
*/
public int add(Connection conn,BookType booktype) throws Exception{
String sql="insert into t_bookType values(null,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, booktype.getBookTypeName());
pstmt.setString(2, booktype.getBookTypeDesc());
return pstmt.executeUpdate();
}
/**
* 查询图书类别集合
* @param conn
* @param bookTypeDao
* @return
*/
public ResultSet list(Connection conn,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=conn.prepareStatement(sb.toString().replace("and", "where"));
return pstmt.executeQuery();
}
/**
* 删除图书类别
* @param conn
* @param id
* @return
* @throws Exception
*/
public int delete(Connection conn,String id) throws Exception{
String sql="delete from t_bookType where id=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 更新图书类别
* @param conn
* @param bookType
* @return
* @throws Exception
*/
public int update(Connection conn,BookType bookType) throws Exception{
String sql="update t_bookType set bookTypeName=?,bookTypeDesc=? where id=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
pstmt.setInt(3, bookType.getId());
return pstmt.executeUpdate();
}
}
UserDao类
package com.java1234.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.java1234.model.User;
/**
* 用户Dao类
* @author Administrator
*
*/
public class UserDao {
/**
* 登陆验证
*
* @param conn
* @param user
* @return
* @throws Exception
*/
public User login(Connection conn,User user) throws Exception{
User resultUser=null;
String sql="select * from t_user where userName=? and password=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
resultUser=new User();
resultUser.setId(rs.getInt("id"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
}
项目主要练习操作sql的增删改查 和一些常用的windowBuilder组件的使用
项目地址.