databasebean.java

来自「精通NetBeans光盘源代码,很好很好的资料」· Java 代码 · 共 102 行

JAVA
102
字号
/*
 * DataBaseBean.java
 *
 * Created on 2006年5月17日, 上午10:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.netbeans.web;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author boyingking
 */
public class DataBaseBean {
    private Connection con;
    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();
        }
    }
    /** Creates a new instance of DataBaseBean */
    public DataBaseBean() {
    }
    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 + =
减小字号Ctrl + -
显示快捷键?