将Access数据库连接到Java Netbeans

时间:2013-01-11 14:03:19

标签: java database ms-access netbeans

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

我被赋予了设计一个简单的超市应用程序的任务,该应用程序有10个项目。用户点击某个项目并选择金额/重量,然后将其添加到底部文本框中的总计。点击后的每个项目都会添加到总和中。

我已经做了尽可能多的技能,但我有两个错误。

  1. 我在程序中的10个项目中手动编写。它们需要通过访问数据库进行连接。我用谷歌搜索了这个方法,发现Netbeans对这个功能有很多帮助,通常只需要一行,比如

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

  2. 我不知道如何连接数据库或如何在连接到Netbeans时提取或使用信息。

    1. 我最初开始编程,一次只能选择一个项目。其余的将变为空白,只有点击项目的总和将出现在下面的框中。对于我的生活,我无法弄清楚如何在点击时添加项目。我很确定这是一个简单的SUM函数,但我很困惑。
    2. 任何建议都将不胜感激。请尽可能简单地解释。我知道基础知识,但我的能力几乎没有延伸到“Hello World”程序lol


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

      }

3 个答案:

答案 0 :(得分:0)

NetBeans只是一个IDE;您没有将任何内容连接到NetBeans。

您的Java代码是连接到Access数据库的内容。

您正在做的是建立JDBC连接。我建议使用JDBC tutorial

听起来你犯了一个经典的学生错误:分解不好。

您可以通过将大问题分解为可以处理的较小问题来解决这些问题。

你有一个用户界面,数据库访问和你的应用程序都在这里进行。我建议你先从数据库访问开始。编写一个处理所需内容的Java对象。彻底测试,然后使用它来完成用户界面的下一步。

答案 1 :(得分:0)

修改您的代码:

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");

答案 2 :(得分:0)

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

这是我连接学生记录项目的代码,希望它可以帮到你。