jsp操作数据库关键类

来源:互联网 发布:申威处理器 知乎 编辑:程序博客网 时间:2024/06/01 07:28
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Minishop;

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;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DataBaseBean {
private Connection con;
private void  connectTODB(){
String classForName="sun.jdbc.odbc.JdbcOdbcDriver"; //获得数据JDBC类
String connectStr="jdbc:odbc:shop_db";
        try {

            Class.forName(classForName);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            this.con = DriverManager.getConnection(connectStr);
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
        }

}

  
    @SuppressWarnings("empty-statement")
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"));
            //product_describe
            vec.add(temppro);
           
            }
           
        } catch (Exception ex) {
            Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
        if(rest!=null)
            try {
                rest.close();
            } catch (SQLException ex) {
                Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if(stmt!=null) try {
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(this.con!=null) try {
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    return vec;
   
}

    if(stmt!=null) try {
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    if(this.con!=null)try {
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBean.class.getName()).log(Level.SEVERE, null, ex);
        }
   
   
}
}
// 操作数据
参考:netbeans -JAVA桌面,WEB与企业级程序开发详解