📄 databasebean.java
字号:
/*
* DataBaseBean.java
*
* Created on 2008年1月30日, 上午8:03
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Adam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
/**
*
* @author acer
*/
public class DataBaseBean {
private Connection con=null;
/** Creates a new instance of DataBaseBean */
public DataBaseBean() {
}
private void connectTODB(){
String CLASSFORNAME="sun.jdbc.odbc.JdbcOdbcDriver";
String CONNECTSTR="jdbc:odbc:shop_db";
try{
Class.forName(CLASSFORNAME);
this.con=DriverManager.getConnection(CONNECTSTR);
}catch(Exception e){
e.printStackTrace();
}
}
public Vector selectProduct(String sql){
Statement stmt=null;
ResultSet rest=null;
Vector vec=new Vector();
this.connectTODB();
try{
stmt=this.con.createStatement();
rest=stmt.executeQuery(sql);
while(rest.next()){
ProductBean temppro=new ProductBean();
temppro.setProductId(rest.getInt("product_id"));
temppro.setProductName(rest.getString("product_name"));
temppro.setProductPrice(rest.getDouble("product_price"));
temppro.setProductNum(rest.getInt("product_num"));
temppro.setProductDescribe(rest.getString("product_describe"));
vec.add(temppro);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rest!=null){
rest.close();
}
if(stmt!=null){
stmt.close();
}
if(this.con!=null){
con.close();
}
}catch(Exception ee){
ee.printStackTrace();
}
}
return vec;
}
public void checkOut(CartBean cartbean){
Statement stmt=null;
Hashtable hash=cartbean.getCartContent();
Enumeration cartEnu=hash.elements();
this.connectTODB();
while(cartEnu.hasMoreElements()){
CartProduct cartpro=(CartProduct)cartEnu.nextElement();
int id=cartpro.getProductId();
int num=cartpro.getSelectedCount();
try{
stmt=con.createStatement();
stmt.executeUpdate("update product set product_num=product_num-"+num+"where product_id="+id+"and product_num>"+num);
}catch(SQLException ex){
ex.printStackTrace();
}
}
try{
if(stmt!=null){
stmt.close();
}
if(this.con!=null){
con.close();
}
}catch(Exception ee){
ee.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -