简体   繁体   English

使用Java在jtable和数据库中添加行

[英]Adding row in jtable and database using Java

I have a problem with my code and it has no error log so I can't see the problem . 我的代码有问题,没有错误日志,所以看不到问题。 . .

Current Problem:
Mainclass() is where the main frame is. . . Clicking ADD will open 
AddBroker() and clicking ADD in AddBroker() will update databse and jtable 
in MainClass(). Database can now be updated but the jtable in MainClass() won't
change until you open it again

This is my main class (other codes were redacted to focus on problem) 这是我的主要课程(已删除其他代码以关注问题)

public class MainClass extends JFrame {
  JButton button_17 = new JButton("ADD");
  button_17.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        //to call class AddBroker()
        AddBroker ab = new AddBroker();
        ab.setVisible(true);

        }
    });}

Then this is the class for AddBroker(). 这是AddBroker()的类。 . .

public class AddBroker extends JFrame {
  JButton btnAdd = new JButton("ADD");
  final Object[] addBrokerrow = new Object[3];
  btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) { 

            ButtonCon bcd = new ButtonCon();
            DriverTableCon dtcd = new DriverTableCon(); //this is just jtable
            MainClass mcd = new MainClass();
            String a = brokerBroker.getText();
            String b = addBroker.getText();
            String c = tinBroker.getText();

            addBrokerrow[0] = a;
            addBrokerrow[1] = b;
            addBrokerrow[2] = c;

            dtcd.modelBroker.addRow(addBrokerrow);
            bcd.addBrokerCon(a,b,c);
        }
    });}

And ButtonCon() is where the adding of data is 而ButtonCon()是添加数据的地方

public class ButtonCon {
 Connection con;
 Statement st;
 ResultSet rs;
 StringBuffer results;
 String url = "jdbc:ucanaccess://C://DATABASE//NTD.accdb";
 public void addBrokerCon(String broker, String add, String tin) {
    try {
        con = DriverManager.getConnection(url);

        String sql = "INSERT INTO brokerT (Broker, Address, Tin_No) VALUES     (?,?,?)";

        ps = con.prepareStatement(sql);

        ps.setString(1, broker);
        ps.setString(2, add);
        ps.setString(3, tin);

        ps.executeUpdate();
        ps.close();
        con.close();

    }

    catch (Exception e) {
            System.out.print(e.toString());
    }
} }

There is no error so I have no idea what is wrong here. 没有错误,所以我不知道这里出了什么问题。 Any input would be appreciated :) 任何输入将不胜感激:)

So it's been a week and what I did try to solve the problem and what I did is add a "Refresh" button that loads the table again. 所以已经过去了一个星期,我确实尝试解决该问题,而我所要做的就是添加一个“刷新”按钮来再次加载表格。

You fogot about ps.executeUpdate(); 您担心ps.executeUpdate(); between ps.setString(3, tin); 之间ps.setString(3, tin); and ps.close(); ps.close(); .

And remove all with st it is not necessary. 并删除所有与st没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM