admindatabasebean.java

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

JAVA
144
字号
/*
 * AdminDataBaseBean.java
 *
 * Created on 2006年5月18日, 下午9:38
 *
 * 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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

/**
 *
 * @author boyingking
 */
public class AdminDataBaseBean {
    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 AdminDataBaseBean */
    public AdminDataBaseBean() {
    }
    public boolean adminLogin(String username,String password){
        boolean loginok=false;
        Statement stmt=null;
        ResultSet rest=null;
        this.connectTODB();
        try {
            stmt=con.createStatement();
            rest=stmt.executeQuery("select * from admin where admin_id like '"+username+"' and password like '"+password+"'");
            if(rest.next()){
                loginok=true;
            }else{
                loginok=false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally{
            this.close(con,stmt,rest);
        }
        return loginok;
    }
    public Vector getAllProduct(){
        Statement stmt=null;
        ResultSet rest=null;
        Vector vec=new Vector();
        this.connectTODB();
        try{
            stmt=this.con.createStatement();
            rest=stmt.executeQuery("select * from product");
            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{
            this.close(con,stmt,rest);
        }  
        return vec;
    }
    public void deleteProduct(String id){
        Statement stmt=null;
        this.connectTODB();
        int tempid=Integer.parseInt(id);
        try{
            stmt=this.con.createStatement();
            stmt.execute("delete from product where product_id="+tempid);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            this.close(con,stmt,null);
        }
    }
    public int addProduct(String id,String name,String price,String num,String describe){
        PreparedStatement ps=null;
        int line=-1;
        int tempid=Integer.parseInt(id);
        String tempname=name;
        double tempprice=Double.parseDouble(price);
        int tempnum=Integer.parseInt(num);
        String tempdesc=describe;
        this.connectTODB();
        try {
            ps=con.prepareStatement("insert into product values(?,?,?,?,?)" );
            ps.setInt(1,tempid);
            ps.setString(2,tempname);
            ps.setDouble(3,tempprice);
            ps.setInt(4,tempnum);
            ps.setString(5,tempdesc);
            line=ps.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }finally{
            try{
                if(ps!=null){
                    ps.close();
                }if(con!=null){
                    con.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }           
        }
        return line;
    }
    private void close(Connection con,Statement stmt,ResultSet rest){
        try {
            if(rest!=null){
                rest.close();
            }
            if(stmt!=null){
                stmt.close();
            }
            if(this.con!=null){
                con.close();
            }
        }catch(Exception ee){
            ee.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?