package com.fangfa;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class DAOBook {
private static final String MYSQL_CON_URL ="jdbc:mysql://localhost:3306/tushuguan";
private static final String USER = "****";
private static final String PASSWORD = "****";
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println("找不到驱动程序!");
}
}
public static Connection getConnection(){
Connection con = null;
try {
con = (Connection) DriverManager.getConnection(MYSQL_CON_URL,USER,PASSWORD);
} catch (SQLException ex) {
System.out.println("数据库连接失败!");
ex.printStackTrace();
}
return con;
}
public Book find(String name){
String sqlStr = "select * from bookxinxitianjia where bookid='" + name + "'";
Book book = null;
try(Connection con = getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sqlStr)){
while(rs.next()){
book = new Book();
book.setId(rs.getInt("bookid"));
book.setTushubianhao(rs.getString("tushubianhao"));
book.setTushumingcheng(rs.getString("tushumingcheng"));
book.setZuozhe(rs.getString("zuozhe"));
book.setYizhe(rs.getString("yizhe"));
book.setJiage(rs.getString("jiage"));
book.setTushuleibie_id(rs.getInt("tushuleibie_id"));
book.setChubanshe_id(rs.getInt("chubanshe_id"));
book.setDinggouxinshu_id(rs.getInt("dinggouxinshu_id"));
}
}catch(SQLException e){
e.printStackTrace();
}
return book;
}
public void add(Book book){
String sqlStr = "insert into bookxinxitianjia values(null,'" + book.getTushubianhao()+"','" + book.getTushumingcheng() + "','" + book.getZuozhe() + "','" + book.getYizhe() + "','" + book.getJiage() + "','" + book.getTushuleibie_id() + "','" + book.getChubanshe_id() + "','" + book.getDinggouxinshu_id() + "')";
try(Connection con = getConnection();
Statement stmt = con.createStatement();){
int result = stmt.executeUpdate(sqlStr);
if(result>0){
JOptionPane.showMessageDialog(null, "添加成功!");
}else{
JOptionPane.showMessageDialog(null, "添加失败!");
}
}catch(SQLException e){
e.printStackTrace();
}
}
public void update(Book book){
String sqlStr = "update bookxinxitianjia set tushumingcheng='" + book.getTushumingcheng() + "',zuozhe='" + book.getZuozhe() + "',yizhe='" + book.getYizhe() + "',jiage='" + book.getJiage() + "',tushuleibie_id='" + book.getTushuleibie_id() + "',chubanshe_id='" + book.getChubanshe_id() + "','2222-22-22' where tushubianhao=" + book.getTushubianhao();
try(Connection con = getConnection();
Statement stmt = con.createStatement();){
int result = stmt.executeUpdate(sqlStr);
if(result>0){
JOptionPane.showMessageDialog(null, "修改成功!");
}else{
JOptionPane.showMessageDialog(null, "修改失败!");
}
}catch(SQLException e){
e.printStackTrace();
}
}
public void remove(Book book){
String sqlStr = "delete from bookxinxitianjia where bookid=" + book.getId();
try(Connection con = getConnection();
Statement stmt = con.createStatement();){
int result = stmt.executeUpdate(sqlStr);
if(result>0){
JOptionPane.showMessageDialog(null, "删除成功!");
}else{
JOptionPane.showMessageDialog(null, "删除失败!");
}
}catch(SQLException e){
e.printStackTrace();
}
}
public List<Book> getBooks(){
List<Book> books = new ArrayList<>();
String sqlStr = "select * from bookxinxitianjia";
try(Connection con = getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sqlStr)){
while(rs.next()){
Book book = new Book();
book.setId(rs.getInt("bookid"));
book.setTushubianhao(rs.getString("tushubianhao"));
book.setTushumingcheng(rs.getString("tushumingcheng"));
book.setZuozhe(rs.getString("zuozhe"));
book.setYizhe(rs.getString("yizhe"));
book.setJiage(rs.getString("jiage"));
book.setTushuleibie_id(rs.getInt("tushuleibie_id"));
book.setChubanshe_id(rs.getInt("chubanshe_id"));
books.add(book);
}
}catch(SQLException e){
e.printStackTrace();
}
return books;
}
}