使用Java在jtable和数据库中添加行(Adding row in jtable and database using Java)
我的代码有问题,它没有错误日志,所以我看不到问题。 。 。
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
这是我的主要课程(其他代码被编辑以关注问题)
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); } });}
然后这是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); } });}
而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()); } } }
没有错误,所以我不知道这里有什么问题。 任何输入将不胜感激:)
所以这已经过去了一个星期,我试图解决这个问题,我做的是添加一个“刷新”按钮,再次加载表格。
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(). . .
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
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.
原文:https://stackoverflow.com/questions/42452621
最满意答案
假设=是在类别行的末尾....
将文件保存到(例如)foo.txt在Solaris上使用nawk,而不是awk。
awk '/=$/ && !arr[$0]++ {print; next} {print $0} ' foo.txt > newfile.txt
抱歉。 我没有清楚的想法如何在没有大量打字工作或使用复杂的ed文件的情况下在vi / vim中执行此操作。 其他人可能有一个想法。 如果你完全陷入困境,请尝试以上方法。
Assuming = is there at the end of category lines....
Save the file to (for example) foo.txt Use nawk on Solaris, not awk.
awk '/=$/ && !arr[$0]++ {print; next} {print $0} ' foo.txt > newfile.txt
Sorry. I do not have a clear idea how to do this in vi/vim without massive typing efforts or using a complex ed file. Someone else may have an idea. If you are totally stuck, try the above.
相关问答
更多-
红帽linux下文本编辑的命令是什么?[2022-08-28]
vim -
UNIX Bash脚本博客建议 : awk '!x[$0]++' 这个命令只是告诉awk要打印哪些行。 变量$0保存一行的全部内容,方括号是数组访问。 因此,对于文件的每一行,如果该节点的内容不是( ! )先前设置的,则数组x的节点将增加并打印行。 The UNIX Bash Scripting blog suggests: awk '!x[$0]++' This command is telling awk which lines to print. The variable $0 holds the ...
-
如果已将vim设置为识别PHP,请在文件顶部键入=G ,它会将代码重新加入到最后。 要扩展rkulla的注释, =是缩进命令,而G是转到文档末尾的命令, gg转到开头。 如果要从当前行开始缩进接下来的5行,可以键入=5=或5== 。 If vim has been set up to recognize PHP, type =G at the top of your file and it will reindent your code to the end. To expand on rkulla's c ...
-
fileformats选项是全局的 。 但是, fileformat选项对每个缓冲区都是本地的。 使用以下命令,您可以检查这些设置的方式: :verbose set ff? ffs? 在我的.vimrc我设置了以下内容: set fileformats=unix,dos,mac 它只是工作。 The fileformats option is global. The fileformat option however is local to each buffer. With the following ...
-
假设=是在类别行的末尾.... 将文件保存到(例如)foo.txt在Solaris上使用nawk,而不是awk。 awk '/=$/ && !arr[$0]++ {print; next} {print $0} ' foo.txt > newfile.txt 抱歉。 我没有清楚的想法如何在没有大量打字工作或使用复杂的ed文件的情况下在vi / vim中执行此操作。 其他人可能有一个想法。 如果你完全陷入困境,请尝试以上方法。 Assuming = is there at the end of categ ...
-
单个命令可能如下所示:v/^M/s/$/\^M/ 。 这使用
,也就是说它会插入一个文字^M字符,该字符用反斜线转义。 A single command might look like :v/^M/s/$/\^M/. This uses , which is to say... it inserts a literal ^M character that's escaped with a backslash. -
如何为vim中的所有文件设置fileformat = unix?(How to set fileformat=unix for all files opend in vim?)[2022-07-06]
也许你需要bufdo ? :help bufdo bufdo[!] {cmd} Execute {cmd} in each buffer in the buffer list. It works like doing this: :bfirst :{cmd} :bnext :{cmd} etc. Maybe you need buf ... -
主题列表表明段名称可能是fugitive:branch而不是fugitive 。 但是,有点猜测。 The theme list suggests maybe the segment name should be fugitive:branch instead of fugitive. Kind of guessing, though.
-
在vim中粘贴文本[重复](Pasting texts in vim [duplicate])[2023-10-06]
使用:set paste以启用粘贴模式。 然后,您可以根据需要粘贴,而无需vim尝试自动添加它。 完成后,请记住使用:set nopaste禁用粘贴模式:set nopaste 。 Use :set paste to enable Paste mode. Then you can paste as much as you want without vim trying to autoindent it. When you are done, remember to disable Paste mode us ... -
在vim中缩进[重复](Indenting in vim [duplicate])[2022-04-23]
首先,对于缩进,有两个>命令,它只是通过添加一个缩进级别向右移动(多少由'shiftwidth'选项确定, 'expandtab'告诉是否使用制表符或空格),并且=命令,适用(特定于语言)缩进规则。 两个命令在加倍时在线上工作,即>> / == ; 通过预先添加数字来解决多行问题。 但他们也采取{motion} ,例如3j (3行向下)或} (到段落的末尾)。 最后,您可以进行视觉选择( V ),使其大小合适,然后将命令应用于它。 First, for indenting there's both the ...