package com.cqipc.tsj.dao;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.cqipc.tsj.model.Back;
import com.cqipc.tsj.model.BookInfo;
import com.cqipc.tsj.model.BookType;
import com.cqipc.tsj.model.Borrow;
import com.cqipc.tsj.model.Operator;
import com.cqipc.tsj.model.Order;
import com.cqipc.tsj.model.Reader;
public class Dao {
protected static String dbClassName = "";
protected static String dbUrl = "";
protected static String dbUser = "";
protected static String dbPwd = "";
private static Connection conn = null;
static {
Properties props = new Properties();
try {
props.load(new FileInputStream("sqlserver.ini"));
dbClassName = props.getProperty("driver");
dbUrl = props.getProperty("url");
dbUser = props.getProperty("userName");
dbPwd = props.getProperty("userPwd");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Dao() {
try {
if (conn == null) {
Class.forName(dbClassName);
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static ResultSet executeQuery(String sql) {
try {
if (conn == null)
new Dao();
return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
} finally {
}
}
public static int executeUpdate(String sql) {
try {
if (conn == null)
new Dao();
return conn.createStatement().executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return -1;
} finally {
}
}
public static void close() {
try {
conn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
conn = null;
}
}
public static Operator check(String name, String password) {
Operator operator = new Operator();
String sql = "select * from tb_operator where name = '" + name + "' and password = '" + password
+ "' and admin=1";
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
operator.setId(rs.getString("id"));
operator.setName(rs.getString("name"));
operator.setGrade(rs.getString("admin"));
operator.setPassword(rs.getString("password"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Dao.close();
return operator;
}
public static List selectBookCategory() {
// TODO Auto-generated method stub
List list = new ArrayList();
String sql = "select * from tb_bookType";
ResultSet rs = executeQuery(sql);
try {
while (rs.next()) {
BookType type = new BookType();
type.setId(rs.getString("id"));
type.setTypeName(rs.getString("typeName"));
type.setDays(rs.getString("days"));
type.setFK(rs.getString("fk"));
list.add(type);
}
} catch (Exception e) {
// TODO: handle exception
}
Dao.close();
return list;
}
public static BookInfo selectBookInfoByISBN(String isbn) {
BookInfo bookInfo = null;
String sql = "select * from tb_bookInfo where ISBN='" + isbn + "'";
ResultSet rs = executeQuery(sql);
try {
while (rs.next()) {
bookInfo = new BookInfo();
bookInfo.setISBN(rs.getString("ISBN"));
bookInfo.setTypeId(rs.getString("typeId"));
bookInfo.setBookName(rs.getString("bookname"));
bookInfo.setWriter(rs.getString("writer"));
bookInfo.setTranslator(rs.getString("translator"));
bookInfo.setPublisher(rs.getString("publisher"));
bookInfo.setDate(rs.getDate("date"));
bookInfo.setPrice(rs.getDouble("price"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Dao.close();
return bookInfo;
}
public static List selectBookInfo() {
List list = new ArrayList();
String sql = "select * from tb_bookInfo";
BookInfo bookInfo;
ResultSet rs = executeQuery(sql);
try {
while (rs.next()) {
bookInfo = new BookInfo();
bookInfo.setISBN(rs.getString("ISBN"));
bookInfo.setTypeId(rs.getString("typeId"));
bookInfo.setBookName(rs.getString("bookname"));
bookInfo.setWriter(rs.getString("writer"));
bookInfo.setTranslator(rs.getString("translator"));
bookInfo.setPublisher(rs.getString("publisher"));
bookInfo.setDate(rs.getDate("date"));
bookInfo.setPrice(rs.getDouble("price"));
list.add(bookInfo);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Dao.close();
return list;
}
/*
* 删除图书信息
*/
public static int deleteBook(String ISBN) {
int i = 0;
try {
String sql = "delete from tb_bookInfo where ISBN='" + ISBN + "'";
i = Dao.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Dao.close();
return i;
}
public static int insertBook(BookInfo bookInfo) {
int i = 0;
// System.out.println(bookInfo.getDate().toString());
try {
String sql = "insert into tb_bookInfo(ISBN,typeId,bookName,writer,translator,publisher,date,price) values('"
+ bookInfo.getISBN() + "','" + bookInfo.getTypeId() + "','" + bookInfo.getBookName() + "','"
+ bookInfo.getWriter() + "','" + bookInfo.getTranslator() + "','" + bookInfo.getPublisher() + "','"
+ new java.sql.Date(bookInfo.getDate().getTime()) + "'," + bookInfo.getPrice() + ")";
// System.out.println(sql);
i = executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Dao.close();
return i;
}
public static int updateBook(BookInfo bookInfo) {
int i = 0;
try {
String sql = "update tb_bookInfo set ISBN='" + bookInfo.getISBN() + "', typeId='" + bookInfo.getTypeId()
+ "', bookName='" + bookInfo.getBookName() + "', writer='" + bookInfo.getWriter()
+ "', translator='" + bookInfo.getTranslator() + "', publisher='" + bookInfo.getPublisher()
+ "', date='" + new java.sql.Date(bookInfo.getDate().getTime()) + "', price=" + bookInfo.getPrice()
+ " where ISBN='" + bookInfo.getISBN() + "'";
// System.out.println(sql);
i = executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Dao.close();
return i;
}
/*
* 查找操作员
*/
public static Operator selectOperator(String name) {
Operator operater = new Operator();
String sql = "select * from tb_operator where name='" + name + "'";
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
operater.setId(rs.getString("id"));
operater.setName(rs.getString("name"));
operater.setGrade(rs.getString("admin"));
operater.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Dao.close();
return operater;
}
public static Operator selectOperator(String name, String password) {
Operator operater = new Operator();
String sql = "select * from tb_operator where name='" + name + "' and password='" + password + "'";
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
operater.setId(rs.getString("id"));
operater.setName(rs.getString("name"));
operater.setGrade(rs.getString("admin"));
operater.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Dao.close();
return operater;
}
/*
* 更改操作员密码
*/
public static int updateOperator(