package connect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.management.j2ee.statistics.Stats;
import lend.lend;
import login.loginBean;
import reader.Reader;
import book.Book;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
public class Connect {
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8";
String sqlname="root";
String sqlpwd="123456";
public Connection getConnection(){
try {
Class.forName(driver);
return DriverManager.getConnection(url, sqlname,sqlpwd);
} catch (Exception e) {
System.out.println("连接数据库异常");
return null;
}
}
public String admin_Login(String pwd,String id){
String sql="select username from admin where password = '"+pwd+"' and admin_id = '"+id+"'";
try {
Statement st=this.getConnection().createStatement();
ResultSet rs=st.executeQuery(sql);
String username = null;
while(rs.next()){
username=rs.getString(1);
}
return username;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("不存在该用户"+e.getMessage());
return null;
}
}
public String reader_Login(String pwd,String id){
String sql="select username from reader_card where password = '"+pwd+"' and reader_id = '"+id+" '";
try {
Statement st=this.getConnection().createStatement();
ResultSet rs=st.executeQuery(sql);
String username = null;
while(rs.next()){
username=rs.getString(1);
}
return username;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("不存在该用户"+e.getMessage());
return null;
}
}
public ArrayList<Book> getBook(){
ArrayList<Book> books=new ArrayList();
String sql="select * from book_info";
try {
Statement st=this.getConnection().createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
Book book=new Book();
book.setBookId(rs.getString("book_id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPublish(rs.getString("publish"));
book.setIsbn(rs.getString("ISBN"));
book.setIntroduction(rs.getString("introduction"));
book.setLanguage(rs.getString("language"));
book.setPrice(rs.getString("price"));
book.setPubdate(rs.getString("pub_date"));
book.setNumber(rs.getString("number"));
books.add(book);
}
return books;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
return null;
}
}
public int updateBook(String name,String price,String number){
String sql="update book_info set price=' "+price+" ',number='"+number+" 'where name='"+name+"'";
int i=0;
try {
Statement st=this.getConnection().createStatement();
i=st.executeUpdate(sql);
/* System.out.print(name);*/
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
return i;
}
}
public Book getbookMessage(String name){
String sql="select * from book_info where name = '"+name+"'";
try {
Statement st=this.getConnection().createStatement();
ResultSet rs=st.executeQuery(sql);
Book book=new Book();
while(rs.next()){
book.setBookId(rs.getString("book_id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPublish(rs.getString("publish"));
book.setIsbn(rs.getString("ISBN"));
book.setIntroduction(rs.getString("introduction"));
book.setLanguage(rs.getString("language"));
book.setPrice(rs.getString("price"));
book.setPubdate(rs.getString("pub_date"));
book.setNumber(rs.getString("number"));
}
return book;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public int deleteBook(String name){
int i=0;
String sql="delete from book_info where name='"+name+"'";
Statement st;
try {
st = this.getConnection().createStatement();
i=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
return i;
}
public boolean getsamBook_name(String name){
String sql="select name from book_info where name='"+name+"'";
try {
Statement st=this.getConnection().createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
if(rs.getString("name").equals(name)){
return true;
}else
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
return false;
}
public int insertBook(String book_id,String name,String author,String publish,String ISBN,String introduction,String language,String price,String pub_date,String number){
int i=0;
String presql="('"+book_id+"', '"+name+"','"+author+"','"+publish+"','"+ISBN+"','"+introduction+"','"+language+"','"+price+"','"+pub_date+"','"+number+"')";
String sql="insert ignore into book_info values "+presql;
try {
Statement st=this.getConnection().createStatement();
i=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
return i;
}
public ArrayList<Reader> getReader(){
ArrayList<Reader> readers=new ArrayList<Reader>();
String sql="select * from reader_info";
try {
Statement st=this.getConnection().createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
Reader reader=new Reader();
reader.setReader_id(rs.getString("reader_id"));
reader.setName(rs.getString("name"));
reader.setSex(rs.getString("sex"));
reader.setPhone(rs.getString("phone"));
reader.setAddress(rs.getString("address"));
reader.setBirth(rs.getString("birth"));
readers.add(reader);
}
return readers;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
return null;
}
}
public Reader getreaderMessage(String reader_id){
String sql="select * from reader_info where reader_id ='"+reader_id+"'";
Reader reader=new Reader();
try {
Statement st=this.getConnection().createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
reader.setReader_id(rs.getString("reader_id"));
reader.setName(rs.getString("name"));
reader.setSex(rs.getString("sex"));
reader.setBirth(rs.getString("birth"));
reader.setAddress(rs.getString("address"));
reader.setPhone(rs.getString("phone"));
}
return reader;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
return null;
}
}
public int updateReader(String reader_id,String birth,String address,String phone){
int i=0;
String sql="update reader_info set birth='"+birth+"' , address='"+address+"' , phone='"+phone+"' where reader_id='"+reader_id+"'";
try {
Statement st=this.getConnection().createStatement();
i=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
return i;
}
public int deleteReader(String reader_id){
int i=0;
String sql="delete from reader_info where reader_id='"+reader_id+"'";
try {
Statement st=this.getConnection().createStatement();
i=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
return i;
}
public ArrayList<lend> getLend(){
ArrayList<lend> lends=new ArrayList<lend>();
String sql="select * from