将Access数据库连接到Java Netbeans

将Access数据库连接到Java Netbeans,java,database,ms-access,netbeans,Java,Database,Ms Access,Netbeans,我目前是一名信息系统专业的学生,在最后一年,我将完成一门编程课程。我不擅长编程,依靠谷歌和课堂笔记来完成最简单的任务 我的任务是设计一个简单的超市应用程序,其中有10种商品。用户单击一个项目并选择数量/重量,然后在底部的文本框中将其添加到总数中。单击时,每个项目都会添加到总和中 我已经尽了我拙劣的技能所能,但我有两个错误 我在程序中手动写入了10项。它们需要通过access数据库连接。我在谷歌上搜索了这个方法,发现Netbeans在这个函数上确实有很大帮助,通常只需要一行,比如 getConne

我目前是一名信息系统专业的学生,在最后一年,我将完成一门编程课程。我不擅长编程,依靠谷歌和课堂笔记来完成最简单的任务

我的任务是设计一个简单的超市应用程序,其中有10种商品。用户单击一个项目并选择数量/重量,然后在底部的文本框中将其添加到总数中。单击时,每个项目都会添加到总和中

我已经尽了我拙劣的技能所能,但我有两个错误

  • 我在程序中手动写入了10项。它们需要通过access数据库连接。我在谷歌上搜索了这个方法,发现Netbeans在这个函数上确实有很大帮助,通常只需要一行,比如

    getConnection(“jdbc:odbc:Driver={Microsoft Access驱动程序(*.mdb)};Dbq=d:\test.mdb”)

  • 我不知道如何连接数据库,也不知道如何在连接到Netbeans时提取或使用信息

  • 首先,我对它进行了编程,一次只能选择一个项目。其余部分将变为空白,只有单击的项目的总和将显示在下面的框中。就我的一生而言,我不知道如何在单击时获得要添加的项目。我很确定这是一个简单的求和函数,但我很困惑
  • 如有任何建议,将不胜感激。请尽可能简单地解释。我知道基本知识,但我的能力几乎没有延伸到“Hello World”程序lol


    导入静态javax.swing.JOptionPane.*;
    导入java.sql.*;
    导入java.awt.*;
    导入java.awt.event.*;
    导入javax.swing.*;
    导入java.text.DecimalFormat;
    公共类Supermarket1扩展JFrame实现ActionListener{
    连接conn=null;
    一磅,两磅,五磅,五磅,五磅,五磅,五磅,十磅,二十磅,五磅,五磅,豆,薄片,糖,茶,咖啡,面包,香肠,鸡蛋,牛奶,土豆;
    JLabel messLabel=新JLabel(“支付金额:”);
    JTextField消息=新的JTextField(10);
    int amount=0;//以便士支付
    十进制英镑=新十进制(£0.00”);
    公共静态void main(字符串[]args){
    超级市场1 c=新的超级市场1();
    c、 setTitle(“超市支付模拟器”);
    c、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    c、 设置大小(500200);
    c、 setVisible(真);
    }
    超级市场1(){
    setLayout(新的BorderLayout());
    oneP=newjbutton(“1p”);oneP.addActionListener(this);
    twoP=newjbutton(“2p”);twoP.addActionListener(this);
    fiveP=newjbutton(“5p”);fiveP.addActionListener(this);
    tenP=newjbutton(“10p”);tenP.addActionListener(this);
    twentyP=newjbutton(“20p”);twentyP.addActionListener(此);
    fiftyP=newjbutton(“50p”);fiftyP.addActionListener(此);
    onePound=新的JButton(£1”);onePound.addActionListener(此);
    twoPounds=新的JButton(£2”);twoPounds.addActionListener(此);
    五磅=新的JButton(£5”);五磅。添加ActionListener(此);
    十磅=新的JButton(£10”);十磅。添加ActionListener(此);
    二十英镑=新的JButton($20);二十英镑。addActionListener(此);
    fiftyPounds=新的JButton(£50”);fiftyPounds.addActionListener(此);
    beans=newjbutton(“烘焙豆”);beans.addActionListener(this);
    flakes=新的JButton(“玉米片”);flakes.addActionListener(此);
    sugar=newjbutton(“sugar”);sugar.addActionListener(this);
    tea=新的JButton(“茶袋”);tea.addActionListener(此);
    coffee=newjbutton(“速溶咖啡”);coffee.addActionListener(这个);
    bread=newjbutton(“bread”);bread.addActionListener(this);
    香肠=新的JButton(“香肠”);香肠.addActionListener(此);
    egg=newjbutton(“egg”);egg.addActionListener(this);
    milk=newjbutton(“milk”);milk.addActionListener(this);
    potato=newjbutton(“potates”);potato.addActionListener(this);
    右按钮();
    JPanel leftSide=新的JPanel();
    setLayout(新的GridLayout(5,1));
    左侧。添加(oneP);
    左侧。添加(twoP);
    左侧。添加(五个);
    左侧。添加(tenP);
    左侧。添加(二十便士);
    左侧。添加(第五种类型);
    左侧。添加(一磅);
    左侧。添加(两磅);
    左边。加上(五磅);
    左边。加上(十磅);
    左边。加上(二十磅);
    左侧。添加(五个输入);
    添加(“西”,左侧);
    JPanel rightSide=新的JPanel();
    右侧.setLayout(新的GridLayout(10,1));
    右边。添加(豆子);
    右侧。添加(薄片);
    右边。加(糖);
    右边。加(茶);
    右侧。添加(咖啡);
    右侧。添加(面包);
    右侧。添加(香肠);
    右侧。添加(鸡蛋);
    右侧。添加(牛奶);
    右侧。添加(土豆);
    添加(“东”,右侧);
    JPanel middle=新的JPanel();
    middle.setLayout(新的FlowLayout());
    中间。添加(标签);
    message.setEditable(false);
    添加(消息);
    添加(“中心”,中间);
    }
    已执行的公共无效操作(操作事件e){
    如果(e.getSource()==beans){amount=35;leftButtons();}
    如果(e.getSource()==flakes){amount=100;leftButtons();}
    如果(e.getSource()==sugar){amount=50;leftButtons();}
    如果(e.getSource()==tea){amount=115;leftButtons();}
    如果(e.getSource()==coffee){amount=250;leftButtons();}
    如果(e.getSource()==bread){amount=50;leftButtons();}
    如果(e.getSource()==香肠){amount=130;leftButtons();}
    如果(e.getSource()==egg){amount=75;leftButtons();}
    如果(e.getSource()==milk){amount=65;leftButtons();}
    如果(e.getSource()==potato){amount=125;leftButtons();}
    如果(e.getSource()==oneP)金额-=1;
    如果(e.getSource()==twoP)金额-=2;
    如果(例如getSource()==fiveP)金额-=5;
    如果(例如getSource()==tenP)金额-=10;
    如果(例如getSource()==twentyP)金额-=20;
    如果(e
    
    import static javax.swing.JOptionPane.*;
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.DecimalFormat;
    
    public class Supermarket1 extends JFrame implements ActionListener {
    Connection conn = null;
    
    
    JButton oneP, twoP, fiveP, tenP, twentyP, fiftyP, onePound, twoPounds, fivePounds,            tenPounds, twentyPounds, fiftyPounds, beans, flakes, sugar, tea, coffee, bread, sausage,   egg, milk, potato;
    JLabel messLabel = new JLabel("Amount to pay:  ");
    JTextField message = new JTextField(10);
    int amount = 0; // payment in pence
    DecimalFormat pounds = new DecimalFormat("£0.00");
    
    public static void main(String[] args) {
        Supermarket1 c = new Supermarket1();
        c.setTitle("Supermarket payment simulator");
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setSize(500, 200);
        c.setVisible(true);
    }
    
    Supermarket1() {
        setLayout(new BorderLayout());
        oneP = new JButton("1p"); oneP.addActionListener(this);
        twoP = new JButton("2p"); twoP.addActionListener(this);
        fiveP = new JButton("5p"); fiveP.addActionListener(this);
        tenP = new JButton("10p"); tenP.addActionListener(this);
        twentyP = new JButton("20p"); twentyP.addActionListener(this);
        fiftyP = new JButton("50p"); fiftyP.addActionListener(this);
        onePound = new JButton("£1"); onePound.addActionListener(this);
        twoPounds = new JButton("£2"); twoPounds.addActionListener(this);
        fivePounds = new JButton("£5"); fivePounds.addActionListener(this);
        tenPounds = new JButton("£10"); tenPounds.addActionListener(this);
        twentyPounds = new JButton("£20"); twentyPounds.addActionListener(this);
        fiftyPounds = new JButton("£50"); fiftyPounds.addActionListener(this);
        beans = new JButton("Baked Beans"); beans.addActionListener(this);
        flakes = new JButton("Corn Flakes"); flakes.addActionListener(this);
        sugar = new JButton("Sugar"); sugar.addActionListener(this);
        tea = new JButton("Tea Bags"); tea.addActionListener(this);
        coffee = new JButton("Instant Coffee"); coffee.addActionListener(this);
        bread = new JButton("Bread"); bread.addActionListener(this);
        sausage = new JButton("Sausage"); sausage.addActionListener(this);
        egg = new JButton("Egg"); egg.addActionListener(this);
        milk = new JButton("Milk"); milk.addActionListener(this);
        potato = new JButton("Potatoes"); potato.addActionListener(this);
    
        rightButtons();
    
        JPanel leftSide = new JPanel();
        leftSide.setLayout(new GridLayout(5, 1));
        leftSide.add(oneP);
        leftSide.add(twoP);
        leftSide.add(fiveP);
        leftSide.add(tenP);
        leftSide.add(twentyP);
        leftSide.add(fiftyP);
        leftSide.add(onePound);
        leftSide.add(twoPounds);
        leftSide.add(fivePounds);
        leftSide.add(tenPounds);
        leftSide.add(twentyPounds);
        leftSide.add(fiftyPounds);
        add("West", leftSide);
    
        JPanel rightSide = new JPanel();
        rightSide.setLayout(new GridLayout(10, 1));
        rightSide.add(beans);
        rightSide.add(flakes);
        rightSide.add(sugar);
        rightSide.add(tea);
        rightSide.add(coffee);
        rightSide.add(bread);
        rightSide.add(sausage);
        rightSide.add(egg);
        rightSide.add(milk);
        rightSide.add(potato);
        add("East", rightSide);
    
        JPanel middle = new JPanel();
        middle.setLayout(new FlowLayout());
        middle.add(messLabel);
        message.setEditable(false);
        middle.add(message);
        add("Center", middle);
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == beans) { amount = 35; leftButtons(); }
        if (e.getSource() == flakes) { amount = 100; leftButtons(); }
        if (e.getSource() == sugar) { amount = 50; leftButtons(); }
        if (e.getSource() == tea) { amount = 115; leftButtons(); }
        if (e.getSource() == coffee) { amount = 250; leftButtons(); }
        if (e.getSource() == bread) { amount = 50; leftButtons(); }
        if (e.getSource() == sausage) { amount = 130; leftButtons(); }
        if (e.getSource() == egg) { amount = 75; leftButtons(); }
        if (e.getSource() == milk) { amount = 65; leftButtons(); }
        if (e.getSource() == potato) { amount = 125; leftButtons(); }
        if (e.getSource() == oneP) amount -= 1;
        if (e.getSource() == twoP) amount -= 2;
        if (e.getSource() == fiveP) amount -= 5;
        if (e.getSource() == tenP) amount -= 10;
        if (e.getSource() == twentyP) amount -= 20;
        if (e.getSource() == fiftyP) amount -= 50;
        if (e.getSource() == onePound) amount -= 100;
        if (e.getSource() == twoPounds) amount -= 200;
        if (e.getSource() == fivePounds) amount -= 500;
        if (e.getSource() == tenPounds) amount -= 1000;
        if (e.getSource() == twentyPounds) amount -= 2000;
        if (e.getSource() == fiftyPounds) amount -= 5000;
    
        if (amount > 0) message.setText(pounds.format(amount / 100.0));
        else {
            message.setText("");
            if (amount < 0) {
                int change = -amount;
                showMessageDialog(this, "Your change is "
                    + pounds.format(change / 100.0)
                    + coins(change),
                    "Change", JOptionPane.INFORMATION_MESSAGE);
            } else {
                showMessageDialog(this, "Thank you",
                    "Exact amount", JOptionPane.INFORMATION_MESSAGE);
            }
            rightButtons();
        }
    }
    
    // enable left buttons, disable right buttons
    void leftButtons() {
        oneP.setEnabled(true);
        twoP.setEnabled(true);
        fiveP.setEnabled(true);
        tenP.setEnabled(true);
        twentyP.setEnabled(true);
        fiftyP.setEnabled(true);
        onePound.setEnabled(true);
        twoPounds.setEnabled(true);
        fivePounds.setEnabled(true);
        tenPounds.setEnabled(true);
        twentyPounds.setEnabled(true);
        fiftyPounds.setEnabled(true);
        beans.setEnabled(true);
        flakes.setEnabled(true);
        sugar.setEnabled(true);
        tea.setEnabled(true);
        coffee.setEnabled(true);
        bread.setEnabled(true);
        sausage.setEnabled(true);
        egg.setEnabled(true);
        milk.setEnabled(true);
        potato.setEnabled(true);
    }
    
    // enable right buttons, disable left buttons
    void rightButtons() {
        oneP.setEnabled(false);
        twoP.setEnabled(false);
        fiveP.setEnabled(false);
        tenP.setEnabled(false);
        twentyP.setEnabled(false);
        fiftyP.setEnabled(false);
        onePound.setEnabled(false);
        twoPounds.setEnabled(false);
        fivePounds.setEnabled(false);
        tenPounds.setEnabled(false);
        twentyPounds.setEnabled(false);
        fiftyPounds.setEnabled(false);
        beans.setEnabled(true);
        flakes.setEnabled(true);
        sugar.setEnabled(true);
        tea.setEnabled(true);
        coffee.setEnabled(true);
        bread.setEnabled(true);
        sausage.setEnabled(true);
        egg.setEnabled(true);
        milk.setEnabled(true);
        potato.setEnabled(true);
    }
    
    String coins(int change) {
        String answer = ":";
        if (change >= 5000) {
            answer += "\nOne £50 note";
            change -= 5000;
        }
        if (change >= 4000) {
            answer += "\nTwo £20 notes";
            change -= 4000;
        }
        if (change >= 2000) {
            answer += "\nOne £20 note";
            change -= 2000;
        }
        if (change >= 1000) {
            answer += "\nOne £10 note";
            change -= 1000;
        }
        if (change >= 500) {
            answer += "\nOne £5 note";
            change -= 500;
        }
        if (change >= 400) {
            answer += "\nTwo £2 coin";
            change -= 400;
        }
        if (change >= 200) {
            answer += "\nOne £2 coin";
            change -= 200;
        }
        if (change >= 100) {
            answer += "\nOne £1 coin";
            change -= 100;
        }
        if (change >= 50) {
            answer += "\nOne 50p coin";
            change -= 50;
        }
        if (change >= 40) {
            answer += "\nTwo 20p coins";
            change -= 40;
        }
        if (change >= 20) {
            answer += "\nOne 20p coin";
            change -= 20;
        }
        if (change >= 10) {
            answer += "\nOne 10p coin";
            change -= 10;
        }
        if (change >= 5) {
            answer += "\nOne 5p coin";
            change -= 5;
        }
        if (change >= 4) {
            answer += "\nTwo 2p coin";
            change -= 4;
        }
        if (change >= 2) {
            answer += "\nOne 2p coin";
            change -= 2;
        }
        if (change >= 1) {
            answer += "\nOne 1p coin";
            change -= 1;
        }
        return answer;
    }
    
    DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\test.mdb");
    
    DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\\test.mdb");
    
    public class StudentFunctions  implements StudentInterface
    {
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=//////URL where your database is located//////";
    
        String query = "";
    
        Connection conn;
        Statement s;
    
    @Override
    public boolean AddStudent(String name, String age, String contact, String program) {
     try {
         Connection conn = DriverManager.getConnection(database, "", "");
           Statement s = conn.createStatement();
            query ="insert into StudentRecord([stname],[age],[contact],[program])values('"+name+"','"+age+"','"+contact+"','"+program+"')";
    
            if(s.executeUpdate(query) > 0)
            {
                 JOptionPane.showMessageDialog(null,"Record successfully add in database");
                return true;
            } else {
                return false;
            }
     } catch (SQLException ex) {
         JOptionPane.showMessageDialog(null,ex.getMessage());
         Logger.getLogger(StudentFunctions.class.getName()).log(Level.SEVERE, null, ex);
     }