如何使用 UCanAccess 从 java GUI 将数据输入到 MS Access

How do I input data from java GUI into MS Access using UCanAccess

我不熟悉使用 UCanAccess 和 Microsoft Access 作为 java:

的数据库
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import net.ucanaccess.jdbc.*;

public class Memo extends JFrame implements ActionListener {

    private JTextField textField;
    private JTextField textField_1;

    Connection cn = null;
    ResultSet rs = null;
    Statement s = null;

    public Memo() {

        getContentPane().setBackground(Color.DARK_GRAY);
        getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setBounds(246, 0, 178, 50);
        getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel = new JLabel("Enter bill amount: $");
        lblNewLabel.setFont(new Font("Arial Narrow", Font.BOLD, 11));
        lblNewLabel.setForeground(Color.WHITE);
        lblNewLabel.setBounds(10, 0, 237, 50);
        getContentPane().add(lblNewLabel);

        JLabel label = new JLabel("Enter water usage amount(l): ");
        label.setFont(new Font("Arial Narrow", Font.BOLD, 11));
        label.setForeground(Color.WHITE);
        label.setBounds(10, 49, 237, 50);
        getContentPane().add(label);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(246, 49, 178, 50);
        getContentPane().add(textField_1);

        JButton btnSubmit = new JButton("Submit");
        btnSubmit.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                try {
                    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                    Connection cn = DriverManager.getConnection("jdbc:ucanaccess://C:\Users\decx\Desktop\Db.accdb");
                    String sql = "insert into db ('ID', 'WaterUsage', 'Bill') + values ('1', '12', '12')";
                    s = cn.createStatement();
                    s.executeUpdate(sql);

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex);
                }
            }
        });
        btnSubmit.setFont(new Font("Arial Narrow", Font.BOLD, 11));
        btnSubmit.setBounds(272, 131, 141, 35);
        getContentPane().add(btnSubmit);

    }

    public static void main(String[] args) throws Exception {

        Memo qMemo = new Memo();
        qMemo.setSize(500, 350);
        qMemo.setVisible(true);
        qMemo.setTitle("Tips & Tricks");
        qMemo.setDefaultCloseOperation(EXIT_ON_CLOSE);
        qMemo.getContentPane().setLayout(null);

    }

    public void actionPerformed(ActionEvent e) {

    }
}

我需要获取点击提交按钮时发送数据的代码。这是一个学校项目,我必须允许用户输入用水量和账单(水电费),以便稍后显示。

我之前有 运行 代码,但出现 "unexpected token" 或 "user has no privilege or object not found" 等错误。

有一些注意事项:

  1. 你得到这个错误 (unexpected token) 因为列名和 table 不应该在两个引号之间 ''
  2. 查询的那个位置不允许+运算符
  3. 此外,只有字符串可以位于两个引号之间,而不是整数,请确保 ID 的类型例如是字符串,否则您必须删除两个引号
  4. 阅读有关 Prepared Statement 的内容以避免语法错误并防止 SQL 注入

看看:

String sql="insert into db ('ID', 'WaterUsage', 'Bill') + values ('1', '12', '12')";
//(1)-----------------------^--^--^----------^--^----^  ^         ^ ^
//(2)___________________________________________________|         | |
//(3)_____________________________________________________________| |

这对我帮助很大,在我朋友的帮助下我完成了我的代码,对于任何可能随时需要它的人,我将包括我的代码。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import net.ucanaccess.jdbc.*;

public class Memo extends JFrame implements ActionListener {

private JTextField textField;
private JTextField textField_1;

Connection cn = null;
ResultSet rs = null;
Statement s = null;

public Memo() {

    getContentPane().setBackground(Color.DARK_GRAY);
    getContentPane().setLayout(null);

    textField = new JTextField();
    textField.setBounds(246, 0, 178, 50);
    getContentPane().add(textField);
    textField.setColumns(10);

    JLabel lblNewLabel = new JLabel("Enter bill amount: $");
    lblNewLabel.setFont(new Font("Arial Narrow", Font.BOLD, 11));
    lblNewLabel.setForeground(Color.WHITE);
    lblNewLabel.setBounds(10, 0, 237, 50);
    getContentPane().add(lblNewLabel);

    JLabel label = new JLabel("Enter water usage amount(l): ");
    label.setFont(new Font("Arial Narrow", Font.BOLD, 11));
    label.setForeground(Color.WHITE);
    label.setBounds(10, 49, 237, 50);
    getContentPane().add(label);

    textField_1 = new JTextField();
    textField_1.setColumns(10);
    textField_1.setBounds(246, 49, 178, 50);
    getContentPane().add(textField_1);


    JButton btnSubmit = new JButton("Submit");
    btnSubmit.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            try {
                int num = Integer.parseInt(textField.getText());
                int num1 = Integer.parseInt(textField_1.getText());

                textField.getText();
                textField_1.getText();


                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                Connection cn = DriverManager.getConnection("jdbc:ucanaccess://C:\Users\DECX\Desktop\Db.accdb");
                String sql = "insert into db (WaterUsage, Bill) values ('"+num+"', '"+num1+"')";
                s = cn.createStatement();
                s.executeUpdate(sql);

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
    });
    btnSubmit.setFont(new Font("Arial Narrow", Font.BOLD, 11));
    btnSubmit.setBounds(272, 131, 141, 35);
    getContentPane().add(btnSubmit);

}

public static void main(String[] args) throws Exception {

    Memo qMemo = new Memo();
    qMemo.setSize(500, 350);
    qMemo.setVisible(true);
    qMemo.setTitle("Memo");
    qMemo.setDefaultCloseOperation(EXIT_ON_CLOSE);
    qMemo.getContentPane().setLayout(null);

}

public void actionPerformed(ActionEvent e) {

}
}