package operation;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import conbean.ConnDBean;
import entity.BookBean;
import entity.UsersBean;
public class BookBeanB {
private Connection con;
private Statement stm;//用于执行SQL语句
private PreparedStatement ps;//用于执行SQL语句(预处理)
private ResultSet rs;//用于保存查询的结果集
public List ArraylistBook(String guanxi){
List list=new ArrayList();
BookBean book=null;
String sql="select * from book where guanxi=?";
try {
con=ConnDBean.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, guanxi);
rs=ps.executeQuery();
while(rs.next()){
book=new BookBean();
book.setId(rs.getInt("id"));
book.setBookname(rs.getString("bookname"));
book.setPhone(rs.getString("phone"));
book.setSex(rs.getString("sex"));
book.setAddress(rs.getString("address"));
book.setGuanxi(rs.getString("guanxi"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public int insertbook(String bookname,String phone,String sex,String address,String guanxi){
int count=0;
con=ConnDBean.getCon();
String sql1="select bookname from book where bookname=?";
String sql="insert into book(bookname,phone,sex,address,guanxi) values(?,?,?,?,?)";
try {
ps=con.prepareStatement(sql1);
ps.setString(1,bookname);
rs=ps.executeQuery();
if(rs.next()){
count=0;
System.out.print("添加失败,该用户名以存在");
}else{
ps=con.prepareStatement(sql);
ps.setString(1,bookname);
ps.setString(2,phone);
ps.setString(3,sex);
ps.setString(4,address);
ps.setString(5,guanxi);
count=ps.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(rs!=null)rs.close();
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
public int delectbook(int id){
int count=0;
String sql="delete from book where id=?";
con=ConnDBean.getCon();
try {
ps=con.prepareStatement(sql);
ps.setInt(1, id);
count=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
public List ArraylistBooka(){
List list=new ArrayList();
BookBean book=null;
String sql="select * from book";
try {
con=ConnDBean.getCon();
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
book = new BookBean();
book.setId(rs.getInt("id"));
book.setBookname(rs.getString("bookname"));
book.setPhone(rs.getString("phone"));
book.setSex(rs.getString("sex"));
book.setAddress(rs.getString("address"));
book.setGuanxi(rs.getString("guanxi"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public int updateBook(String phone,String address,String guanxi,int id){
int count=0;
String sql="update book set phone=?,address=?,guanxi=? where id=? ";
con=ConnDBean.getCon();
try {
ps=con.prepareStatement(sql);
ps.setString(1,phone);
ps.setString(2, address);
ps.setString(3,guanxi);
ps.setInt(4,id);
count=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(ps!=null)ps.close();
if(con!=null)con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
}
- 1
- 2
前往页