将 Access 数据库连接到 Java Netbeans
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14279739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting Access Database to Java Netbeans
提问by user1970142
I am currently a Information System Student which has been given a programming final year coursework to complete. I am not in any way good at programming and depend on google and class notes to perform the simplest of tasks.
我目前是一名信息系统学生,已经完成了最后一年的编程课程。我一点也不擅长编程,依靠谷歌和课堂笔记来执行最简单的任务。
I was given the task to design a simple Supermarket application which has 10 items. The user clicks on an item and selects the amount/weight which then gets added to a total at a text box at the bottom. Each item when clicked gets added to the sum.
我的任务是设计一个简单的 Supermarket 应用程序,其中包含 10 个项目。用户单击一个项目并选择数量/重量,然后将其添加到底部文本框中的总数中。单击时的每个项目都会添加到总和中。
I have already done as much as my poor skills can do but i have two errors.
我已经尽了我的拙劣技能所能做的,但我有两个错误。
I manually wrote in the 10 items within the program. They need to be connected through an access database. I have googled this method and found that Netbeans does help a lot with this function and usually only one line is needed such as
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\test.mdb");
我在程序内手动写了10个项目。它们需要通过访问数据库连接。我用谷歌搜索了这个方法,发现 Netbeans 确实对这个功能有很大帮助,通常只需要一行,例如
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\test.mdb");
I have no clue how to connect the database or how to extract or use the information while it is connected to Netbeans.
我不知道如何连接数据库,也不知道如何在连接到 Netbeans 时提取或使用信息。
- I had initially, to start, programmed it in such a way that only one item could be selected at a time. The rest would go blank and only the sum of the item clicked would appear in the box below. For the life of me, I cant figure out how to get the items to add when clicked. I am pretty sure it is a simple SUM function but I am just so confused.
- 一开始,我对它进行了编程,每次只能选择一个项目。其余部分将变为空白,只有点击的项目的总和会出现在下面的框中。对于我的一生,我无法弄清楚如何在单击时添加要添加的项目。我很确定这是一个简单的 SUM 函数,但我很困惑。
Any advice would be appreciated. Please explain as simply as possible. I know the basics but my capabilities barely stretch as far as the "Hello World" program lol
任何意见,将不胜感激。请尽可能简单地解释。我知道基础知识,但我的能力几乎没有延伸到“Hello World”程序,哈哈
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;
}
}
}
回答by duffymo
NetBeans is just an IDE; you don't connect anything to NetBeans.
NetBeans 只是一个 IDE;您没有将任何东西连接到 NetBeans。
Your Java code is what is connecting to the Access database.
您的 Java 代码是连接到 Access 数据库的内容。
What you're doing is making a JDBC connection. I'd recommend a JDBC tutorial.
您正在做的是建立 JDBC 连接。我会推荐一个JDBC 教程。
It sounds like you're committing a classic student mistake: poor decomposition.
听起来您犯了一个典型的学生错误:分解不良。
You solve big problems by breaking them into smaller ones that you can handle.
您可以通过将大问题分解为您可以处理的小问题来解决它们。
You've got a user interface, database access, and your application all going on here. I'd recommend that you start with the database acces first. Write one Java object that handles what you need. Test it thoroughly and then use it to take the next step with your user interface.
你有一个用户界面、数据库访问和你的应用程序都在这里进行。我建议您首先从数据库访问开始。编写一个处理您需要的 Java 对象。彻底测试它,然后使用它对您的用户界面进行下一步。
回答by Sawan Budhbhatti
Modify your code from:
修改您的代码:
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\test.mdb");
To using a double backslash (\\
)
使用双反斜杠 ( \\
)
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\test.mdb");
回答by sam
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);
}
This is code in which I connected access to my project of student record, hope it might help you.
这是我连接访问我的学生记录项目的代码,希望它可以帮助您。