java向Access中插入数据只有部分数据能插入

今天我向Access插入数据过程中插入account和password两个数据,可只有一个数据被插入,但insert代码在Access里进行就能正常插入,请问这是什么原因?

这是我的sql代码

img


但在Access里出现数据缺少,密码到了account里面,后面直接是空

img


但同样的代码在Access进行sql查询就能完整的储存

img

img


这是设计视图

img

我遇见过这种情况,应该是你jdbc不完整

不放java代码不知道是哪里不对啊?目前看sql没有问题,应该是java那里写错了


package Staffing_system;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.*;
import java.util.Random;
import java.io.*;

/*
 随机分配10位长度数字账号
 密码可自行设置
 */
public class register extends JFrame implements ActionListener {
    JLabel title;
    JLabel accountLabel;
    JLabel passwordLabel;
    JLabel account;
    JPasswordField password;
    JButton register;
    long num;
    register(String s) {
        super(s);
        this.setSize(300, 200);
        // 得到显示器屏幕的宽、高
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        // 得到窗体的宽、高
        int windowsWidth = this.getWidth();
        int windowsHeight = this.getHeight();
        this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(2);
        init();
        this.setVisible(true);
    }

    void init() {
        title = new JLabel("账户注册", JLabel.CENTER);
        title.setForeground(Color.blue);
        accountLabel = new JLabel("账号: ");
        passwordLabel = new JLabel("密码: ");
        account = new JLabel("");
        password = new JPasswordField(10);
        register = new JButton("注册");
        getAccount();
        addActionListener();
        setLayout();
    }

    // 获取随机10位账号
    public void getAccount() {
        Random random = new Random();
        num = random.nextInt(9) + 1;// 第一位不能为0
        for (int i = 1; i <= 9; i++) {
            num = num * 10 + random.nextInt(10);
        }
        account.setText(String.valueOf(num));
    }

    // 添加注册事件
    public void addActionListener() {
        register.addActionListener(this);
    }

    public void setLayout() {
        Box box = Box.createVerticalBox();
        Box box1 = Box.createHorizontalBox();
        Box box2 = Box.createHorizontalBox();
        Box box3 = Box.createHorizontalBox();
        Box box4 = Box.createHorizontalBox();
        box1.add(title);
        box2.add(accountLabel);
        box2.add(account);
        box3.add(passwordLabel);
        box3.add(password);
        box4.add(new JLabel("        "));
        box4.add(new JLabel("  "));
        box4.add(register);
        box.add(box1);
        box.add(box2);
        box.add(box3);
        box.add(box4);
        this.add(box);
    }

    public static void main(String[] args) {
        new register("账户注册");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 连接数据库
        Connection con = null;
        Statement statement = null;
        ResultSet rs = null;
        boolean flag = false;
        try {
            Class.forName("com.hxtt.sql.access.AccessDriver");
            con = DriverManager.getConnection("jdbc:Access:///User1.accdb");
        } catch (Exception df) {
            JOptionPane.showMessageDialog(null, "数据库连接失败");
        }
        try {
            statement = con.createStatement();// 创建操作数据库语句
            String sql="insert into Account(account,password) values('"+String.valueOf(num)+"','"+password.getText()+"')";
            System.out.println(sql);
            statement.executeUpdate(sql);//存储数据
            con.close();
            JOptionPane.showMessageDialog(null,"注册成功!");
        } catch (Exception f) {
            JOptionPane.showMessageDialog(null, "SQL语句错误!");
        }

        

    }
}

获取密码那里改成password.getPassword().toString().trim()
试试
trim()去掉空格

关于该问题,我找了一篇非常好的博客,你可以看看是否有帮助,链接:java读取Access数据

问题描述中哪儿跟java沾边了

看一下接口执行时,控制台打印的,实际运行的sql

你这个是拼接sql , 你用占位符做不容易出问题
String sql = "insert into Account(account,password) values(?,?)";
statement = con.prepareStatement(sql);
((PreparedStatement) statement).setString(1, String.valueOf(num));
((PreparedStatement) statement).setString(2, password.getText());