⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 admindatabasebean.java

📁 基于netbeans的java桌面应用程序合集
💻 JAVA
字号:
/*
 * AdminDataBaseBean.java
 *
 * Created on 2008年2月1日, 上午7:28
 *
 * 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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

/**
 *
 * @author acer
 */
public class AdminDataBaseBean {
    private Connection con=null;
    private void connetcTODB(){
        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() {
    }

    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();
       }
    }

    public boolean adminLogin(String username, String password) {
        boolean loginok=false;
        Statement stmt=null;
        ResultSet rest=null;
        this.connetcTODB();
        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.connetcTODB();
        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.connetcTODB();
        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.connetcTODB();
        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(Exception ex){
            ex.printStackTrace();
        }finally{
            try{
                if(ps!=null){
                    ps.close();
                }if(con!=null){
                    con.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return line;
    }
   
}

⌨️ 快捷键说明

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