一、环境搭建
eclipse+mysql+swing
二、界面展示
三、代码清单
Jdbc连接数据库
package com.h.Util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil {
private String url="jdbc:mysql://localhost:3306/libarary";
private String userName="root";
private String password="";
private String jdbcName="com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getConnection() throws Exception {
Class.forName(jdbcName);
Connection con= DriverManager.getConnection(url,userName,password);
return con;
}
/**
* 关闭数据库
* @throws Exception
*/
public void closeCollection(Connection con) throws Exception {
if(con!=null) {
con.close();
}
}
public static void main(String[] args) {
DbUtil dbUtil=new DbUtil();
try {
dbUtil.getConnection();
System.out.println("sucess");
} catch (Exception e) {
e.printStackTrace();
System.out.println("fail");
}
}
}
package com.h.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.h.Util.StringUtil;
import com.h.model.Book;
/**
* 图书Dao类
* @author Administrator
*
*/
public class BookDao {
/**
* 图书添加
* @param con
* @param book
* @return
* @throws Exception
*/
public int add(Connection con,Book book)throws Exception {
String sql="insert into book values(null,?,?,?,?,?,?)";
PreparedStatement pstm= con.prepareStatement(sql);
pstm.setString(1, book.getBookName());
pstm.setString(2,book.getAuthor());
pstm.setString(3,book.getSex());
pstm.setFloat(4, book.getPrice());
pstm.setInt(5, book.getBookTypeId());
pstm.setString(6, book.getBookDesc());
return pstm.executeUpdate();
}
/**
* 图书信息查询
*/
public ResultSet list(Connection con,Book book) throws Exception{
StringBuffer sb=new StringBuffer("select *from book b,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 like '%"+book.getBookTypeId()+"%'");
}
PreparedStatement pstm= con.prepareStatement(sb.toString());
return pstm.executeQuery();
}
/**
* 图书类别删除
*/
public int delete(Connection con,String id) throws Exception {
String sql="delete from book where id=?";
PreparedStatement pstm=con.prepareStatement(sql);
pstm.setString(1,id);
return pstm.executeUpdate();
}
/**
*
*/
public int update(Connection con,Book book)throws Exception {
String sql="update book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=?";
PreparedStatement pstm=con.prepareStatement(sql);
pstm.setString(1,book.getBookName());
pstm.setString(2,book.getAuthor());
pstm.setString(3,book.getSex());
pstm.setFloat(4, book.getPrice());
pstm.setString(5,book.getBookDesc());
pstm.setInt(6,book.getBookTypeId());
pstm.setInt(7,book.getId());
return pstm.executeUpdate();
}
/**
* 判断指定图书类别下是否有图书
* @param con
* @param bookTypeId
* @return
* @throws Exception
*/
public boolean isExistBookTypeId(Connection con,String bookTypeId) throws Exception{
String sql="select * from book where bookTypeId=?";
PreparedStatement pstm=con.prepareStatement(sql);
pstm.setString(1, bookTypeId);
ResultSet rs= pstm.executeQuery();
return rs.next();
}
}
package com.h.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.h.Util.StringUtil;
import com.h.model.BookType;
/**
* 图书类别
* @author Administrator
*
*/
public class BookTypeDao {
/**\
* 图书类别添加
* @param con
* @param bookType
* @return
* @throws Exception
* @throws Exception
*/
public int add(Connection con,BookType bookType) throws Exception {
String sql="insert into booktype values(null,?,?)";
PreparedStatement pstm= con.prepareStatement(sql);
pstm.setString(1,bookType.getBookTypeName());
pstm.setString(2, bookType.getBookTypeDesc());
return pstm.executeUpdate();
}
/**
* 查询图书类别
* @param con
* @param bookType
* @return
*/
public ResultSet list(Connection con,BookType bookType) throws Exception{
StringBuffer stringBuffer=new StringBuffer("select * from booktype");
if(StringUtil.isNotEmpty(bookType.getBookTypeName())) {
stringBuffer.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
}
PreparedStatement pstm= con.prepareStatement(stringBuffer.toString().replaceFirst("and", "where"));
return pstm.executeQuery();
}
/**
* 删除图书类别
*/
public int delete(Connection con,String id)throws Exception {
String sql="delete from booktype where id=?";
PreparedStatement pstm=con.prepareStatement(sql);
pstm.setString(1, id);
return pstm.executeUpdate();
}
/**
* 修改图书类别
*/
public int update(Connection con,BookType bookType)throws Exception {
String sql="update booktype set bookTypeName=?,bookTypeDesc=? where id=?";
PreparedStatement pstm= con.prepareStatement(sql);
pstm.setString(1, bookType.getBookTypeName());
pstm.setString(2, bookType.getBookTypeDesc());
pstm.setInt(3, bookType.getId());
return pstm.executeUpdate();
}
}
package com.h.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.h.model.User;
/**
* 操作数据库返回用户数据
* @author Administrator
*
*/
public class UserDao {
public User login(Connection con,User user) throws Exception {
User resUser=null;
String sql="select *from user where userName=?and password=?";
PreparedStatement pstm= con.prepareStatement(sql);
pstm.setString(1, user.getUserName());
pstm.setString(2, user.getPassword());
ResultSet rs= pstm.executeQuery();
if(rs.next()) {
resUser=new User();
resUser.setId(rs.getInt("id"));
resUser.setUserName(rs.getString("username"));
resUser.setPassword(rs.getString("password"));
}
return resUser;
}
}
源码链接添加链接描述