📄 estorebooks.java
字号:
package estore;
import java.sql.*;
import java.util.*;
/**
* Class <b>EStoreBooks</b> stores
* all of the books currently in the E-Store.
*
* @author A Jiayi
* @version 1.0
*/
public class EStoreBooks {
//Database connection parameter
private final String driver = "org.postgresql.Driver";
private final String url = "jdbc:postgresql:E-StoreDB";
private final String user = "webuser";
private final String passwd = "webuser";
//Hashtable stores the title and isbn
//of all books in the E-Store.
private Hashtable tempBooks = new Hashtable(15, 8);
/**
* Constructor.Connect database and store
* the title and isbn of all books into two arrays.
*/
public EStoreBooks() throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection conn = DriverManager.getConnection( url, user, passwd);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT name, isbn FROM title5");
while (rset.next()) {
tempBooks.put(rset.getString("name"), rset.getString("isbn"));
}
rset.close();
stmt.close();
conn.close();
}
/**
* Get the isbns of all books.
* @param null
* @return String[] isbns
*/
public String[] getIsbns() {
int i = 0;
String[] isbns = new String[tempBooks.size()];
for (Enumeration isbnEnum = tempBooks.keys(); isbnEnum.hasMoreElements(); ) {
isbns[i++] = (String)isbnEnum.nextElement();
}
return isbns;
}
/**
* Get the titles of all books.
* @param null
* @return String[] titles
*/
public String[] getTitles() {
int i = 0;
String[] titles = new String[tempBooks.size()];
for (Enumeration isbnEnum = tempBooks.keys(); isbnEnum.hasMoreElements(); ) {
titles[i++] = (String)tempBooks.get(isbnEnum.nextElement());
}
return titles;
}
/**
* Get the title of a book gived isbn.
* @param String isbn
* @return String title
*/
public String getTitle(String isbn) {
return (String)tempBooks.get(isbn);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -