LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 767|回复: 4

cannot resolve symbol的老问题

[复制链接]
发表于 2004-12-26 10:06:15 | 显示全部楼层 |阅读模式
我是java的初学者,请教各位大侠一个小问题。我的环境是Red Hat Enterprise Linux AS release 3 (Taroon)+tomcat-5.0.12+java 1.4.1_02。编译4个java文件,ShoppingCartItem.java、BookDetails.java、BookDB.java、ShoppingCart.java。出现以下错误。
/www/jakarta-tomcat-5.0.12/webapps/bookstore/src/mypack/ShoppingCart.java:4: cannot resolve symbol
symbol : class BookDetails
location: package mypack
import mypack.BookDetails;

java的环境如下:
JAVA_HOME=/usr/local/java
CLASSPATH=.:/usr/local/java/lib:/usr/local/java/lib/dt.jar:/usr/local/java/bin:/usr/local/java/lib/tools.jar
编译文件如下:
export catalina_home=/www/jakarta-tomcat-5.0.12
export src=$catalina_home/webapps/bookstore/src/mypack
export dest=$catalina_home/webapps/bookstore/WEB-INF/classes
export destlib=$catalina_home/webapps/bookstore/WEB-INF/lib
export classpath=$catalina_home/common/lib/servlet-api.jarcatalina_home/common/lib/jsp-api.jardestlib/mysqldriver.jar
javac -sourcepath $src -classpath $classpath -d $dest $src/BookDetails.java
javac -sourcepath $src -classpath $classpath -d $dest $src/ShoppingCartItem.java
javac -sourcepath $src -classpath $classpath -d $dest $src/ShoppingCart.java
javac -sourcepath $src -classpath $classpath -d $dest $src/BookDB.java
 楼主| 发表于 2004-12-26 10:07:43 | 显示全部楼层
源文件如下:
package mypack;

import java.util.*;
import mypack.*;

public class ShoppingCart {
HashMap items = null;
int numberOfItems = 0;

public ShoppingCart() {
items = new HashMap();
}

public synchronized void add(String bookId, BookDetails book) {
if(items.containsKey(bookId)) {
ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
scitem.incrementQuantity();
} else {
ShoppingCartItem newItem = new ShoppingCartItem(book);
items.put(bookId, newItem);
}

numberOfItems++;
}

public synchronized void remove(String bookId) {
if(items.containsKey(bookId)) {
ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
scitem.decrementQuantity();

if(scitem.getQuantity() <= 0)
items.remove(bookId);

numberOfItems--;
}
}

public synchronized Collection getItems() {
return items.values();
}

protected void finalize() throws Throwable {
items.clear();
}

public synchronized int getNumberOfItems() {
return numberOfItems;
}
public synchronized double getTotal() {
double amount = 0.0;

for(Iterator i = getItems().iterator(); i.hasNext(); ) {
ShoppingCartItem item = (ShoppingCartItem) i.next();
BookDetails bookDetails = (BookDetails) item.getItem();

amount += item.getQuantity() * bookDetails.getPrice();
}
return roundOff(amount);
}

private double roundOff(double x) {
long val = Math.round(x*100); // cents
return val/100.0;
}

public synchronized void clear() {
items.clear();
numberOfItems = 0;
}
}
 楼主| 发表于 2004-12-26 10:08:57 | 显示全部楼层
ShoppingCartItem.java
package mypack;

public class ShoppingCartItem {
Object item;
int quantity;

public ShoppingCartItem(Object anItem) {
item = anItem;
quantity = 1;
}

public void incrementQuantity() {
quantity++;
}

public void decrementQuantity() {
quantity--;
}

public Object getItem() {
return item;
}

public int getQuantity() {
return quantity;
}
}
 楼主| 发表于 2004-12-26 10:09:40 | 显示全部楼层
BookDetails.java
package mypack;

public class BookDetails implements Comparable {
private String bookId = null;
private String title = null;
private String name = null;
private float price = 0.0F;
private int year = 0;
private String description = null;
private int saleAmount;

public BookDetails(String bookId, String name, String title,
float price, int year, String description,int saleAmount) {
this.bookId = bookId;
this.title = title;
this.name = name;
this.price = price;
this.year = year;
this.description = description;
this.saleAmount=saleAmount;

}

public String getTitle() {
return title;
}

public float getPrice() {
return price;
}

public int getYear() {
return year;
}

public String getDescription() {
return description;
}

public String getBookId() {
return this.bookId;
}

public String getName() {
return this.name;
}

public int getSaleAmount(){
return this.saleAmount;
}
public int compareTo(Object o) {
BookDetails n = (BookDetails)o;
int lastCmp = title.compareTo(n.title);
return (lastCmp);
}
}
 楼主| 发表于 2004-12-26 10:15:04 | 显示全部楼层
BookDB.java
/** access mysql database through JDBC Driver */
package mypack;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;
import mypack.*;

public class BookDB {

private ArrayList books;
private String dbUrl = "jdbc:mysql://localhost:3306/bookdb";
private String dbUser="dbuser";
private String dbPwd="1234";

public BookDB () throws Exception{
Class.forName("com.mysql.jdbc.Driver");
}

public Connection getConnection()throws Exception{
return java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
}

public void closeConnection(Connection con){
try{
if(con!=null) con.close();
}catch(Exception e){
e.printStackTrace();
}
}

public void closePrepStmt(PreparedStatement prepStmt){
try{
if(prepStmt!=null) prepStmt.close();
}catch(Exception e){
e.printStackTrace();
}
}

public void closeResultSet(ResultSet rs){
try{
if(rs!=null) rs.close();
}catch(Exception e){
e.printStackTrace();
}
}

public int getNumberOfBooks() throws Exception {
Connection con=null;
PreparedStatement prepStmt=null;
ResultSet rs=null;
books = new ArrayList();

try {
con=getConnection();
String selectStatement = "select * " + "from books";
prepStmt = con.prepareStatement(selectStatement);
rs = prepStmt.executeQuery();

while (rs.next()) {
BookDetails bd = new BookDetails(rs.getString(1), rs.getString(2), rs.getString(3),
rs.getFloat(4), rs.getInt(5), rs.getString(6),rs.getInt(7));
books.add(bd);
}

}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
closeConnection(con);
}
return books.size();
}

public Collection getBooks()throws Exception{
Connection con=null;
PreparedStatement prepStmt=null;
ResultSet rs =null;
books = new ArrayList();
try {
con=getConnection();
String selectStatement = "select * " + "from books";
prepStmt = con.prepareStatement(selectStatement);
rs = prepStmt.executeQuery();

while (rs.next()) {

BookDetails bd = new BookDetails(rs.getString(1), rs.getString(2), rs.getString(3),
rs.getFloat(4), rs.getInt(5), rs.getString(6),rs.getInt(7));
books.add(bd);
}

}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
closeConnection(con);
}

Collections.sort(books);
return books;
}

public BookDetails getBookDetails(String bookId) throws Exception {
Connection con=null;
PreparedStatement prepStmt=null;
ResultSet rs =null;
try {
con=getConnection();
String selectStatement = "select * " + "from books where id = ? ";
prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, bookId);
rs = prepStmt.executeQuery();

if (rs.next()) {
BookDetails bd = new BookDetails(rs.getString(1), rs.getString(2), rs.getString(3),
rs.getFloat(4), rs.getInt(5), rs.getString(6),rs.getInt(7));
prepStmt.close();

return bd;
}
else {
return null;
}
}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
closeConnection(con);
}
}

public void buyBooks(ShoppingCart cart)throws Exception {
Connection con=null;
Collection items = cart.getItems();
Iterator i = items.iterator();
try {
con=getConnection();
con.setAutoCommit(false);
while (i.hasNext()) {
ShoppingCartItem sci = (ShoppingCartItem)i.next();
BookDetails bd = (BookDetails)sci.getItem();
String id = bd.getBookId();
int quantity = sci.getQuantity();
buyBook(id, quantity,con);
}
con.commit();
con.setAutoCommit(true);

} catch (Exception ex) {
con.rollback();
throw ex;
}finally{
closeConnection(con);
}
}


public void buyBook(String bookId, int quantity,Connection con) throws Exception {
PreparedStatement prepStmt=null;
ResultSet rs=null;
try{
String selectStatement = "select * " + "from books where id = ? ";
prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, bookId);
rs = prepStmt.executeQuery();

if (rs.next()) {
prepStmt.close();
String updateStatement =
"update books set saleamount = saleamount + ? where id = ?";
prepStmt = con.prepareStatement(updateStatement);
prepStmt.setInt(1, quantity);
prepStmt.setString(2, bookId);
prepStmt.executeUpdate();
prepStmt.close();
}

}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
}
}
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表