📄 keepbook.java
字号:
/*
* Keepbook.java
*
* Created on 2007年7月26日, 上午9:28
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package czm;
import java.sql.*;
import java.sql.Connection;
/**
*
* @author Administrator
*/
public class Keepbook {
public String userID;//用户帐号
public String bookid;//书本isbn
public String keepdate;//收藏日期
public String bookname;//
public String price;//
private static String strDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; //JDBC驱动
private static String strDBUrl = "jdbc:odbc:bookstore"; //数据源 ,
private Connection conn =null; //连接
private ResultSet rs = null;
/** Creates a new instance of Keepbook */
public Keepbook() {
//加载JDBC-ODBC驱动
try {
Class.forName(strDBDriver );
conn = DriverManager.getConnection(strDBUrl);
}
//捕获异常
catch(java.lang.ClassNotFoundException cnfe){
System.err.println("Keepbook():" + cnfe.getMessage());
}catch(java.lang.Exception e){
System.err.println("Keepbook():" + e.getMessage());
}
}
private ResultSet sqlExecuteQuery(String sql){
try{
//创建可滚动的结果集。
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs;
}catch(Exception ex){
return null;
}
}
private boolean Isexist(String userID,String bookid) {//检查是否有相同的藏书
try {
if(userID == null || bookid == null || userID.equals("") || bookid.equals(""))
return true;
String sql="select BookISBN from Keepbook where memberID='"+userID+"'";
ResultSet rs=sqlExecuteQuery(sql);
while(rs.next()){
String tempbookid = rs.getString("BookISBN");
if(tempbookid.equals(bookid))
return true;
}
return false;
} catch(Exception exc) {
return true;
}
}
private void sqlExecuteUpdate(String sql)throws Exception{
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
}
public boolean Deletebook(String userID,String bookid) {/**删除*/
try {
if(userID == null || bookid == null || userID.equals("") || bookid.equals(""))
return false;
String sql="delete from Keepbook where memberID='"+userID+"' and BookISBN='"+bookid+"'";
sqlExecuteUpdate(sql);
return true;
} catch(Exception exc) {
return false;
}
}
public boolean DeleteAllBook(String userID) {/**删除所有*/
try {
if(userID == null || userID.equals(""))
return false;
String sql="delete from Keepbook where memberID='"+userID+"'";
sqlExecuteUpdate(sql);
return true;
} catch(Exception exc) {
return false;
}
}
public boolean Inserbook(String userID,String bookid) {/**插入 */
try {
//检查是否有相同的用户
if(Isexist(userID,bookid)) return false;
System.out.println((new java.util.Date()).toLocaleString());
String sql = "insert into Keepbook values('"+userID+"','"+bookid+"',#"+(new java.util.Date()).toLocaleString()+"#)";
sqlExecuteUpdate(sql);
return true;
} catch(Exception exc) {
System.out.println((new java.util.Date()).toLocaleString());
return false;
}
}
public ResultSet Showbook(String userID) {/**显示*/
try {
if(userID == null || userID.equals(""))
return null;
String sql = "select Keepbook.BookISBN,bookinfo.bookName,Keepbook.keepdate,bookinfo.price from Keepbook,bookinfo where Keepbook.memberID='"+userID+"' and bookinfo.bookISBN=Keepbook.BookISBN order by Keepbook.keepdate desc";
return sqlExecuteQuery(sql);
} catch(Exception Exc) {
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -