?? product.java
字號:
package ajax.biz;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import ajax.db.DBUtils;
public class Product {
private String productId = null; //產(chǎn)品編號
private String productName = null; //產(chǎn)品名稱
private double productPrice = 0; //產(chǎn)品價(jià)格
private int productsNum = 0; //產(chǎn)品數(shù)量
/**
* 通過產(chǎn)品ID構(gòu)造一個(gè)產(chǎn)品
*/
public Product(String productId) {
String sql = "select pid, pname, price from products where pid = ?";
Connection conn = null; //聲明Connection對象
PreparedStatement pstmt = null; //聲明PreparedStatement對象
ResultSet rs = null; //聲明ResultSet對象
try {
conn = DBUtils.getConnection(); //獲取數(shù)據(jù)庫連接
pstmt = conn.prepareStatement(sql); //根據(jù)sql創(chuàng)建PreparedStatement
pstmt.setString(1, productId); //設(shè)置參數(shù)
rs = pstmt.executeQuery(); //執(zhí)行查詢,返回結(jié)果集
if (rs.next()) {
this.productId = rs.getString(1);
this.productName = rs.getString(2);
this.productPrice = rs.getDouble(3);
this.productsNum = 1;
}
} catch (SQLException e) {
System.out.println(e.toString());
} finally {
DBUtils.close(rs); //關(guān)閉結(jié)果集
DBUtils.close(pstmt); //關(guān)閉PreparedStatement
DBUtils.close(conn); //關(guān)閉連接
}
}
//獲取產(chǎn)品編號
public String getProductId() {
return productId;
}
//設(shè)置產(chǎn)品編號
public void setProductId(String productId) {
this.productId = productId;
}
//獲取產(chǎn)品名稱
public String getProductName() {
return productName;
}
//獲取產(chǎn)品價(jià)格
public double getProductPrice() {
return productPrice;
}
//獲取產(chǎn)品數(shù)量
public int getProductsNum() {
return productsNum;
}
//設(shè)置產(chǎn)品數(shù)量
public void setProductsNum(int productsNum) {
this.productsNum = productsNum;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -