使用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
2023-01-24 18:01

满意答案

假设=是在类别行的末尾....

将文件保存到(例如)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.

相关问答

更多

Unix:删除不排序的重复行(Remove duplicate lines without sorting [duplicate])

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 hold...

用于格式化PHP代码的Unix或VIM命令(Unix or VIM command to format PHP code)

如果已将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 rkull...

Vim:更改特定目录的文件格式[重复](Vim: Change the file format for a particular directory [duplicate])

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 foll...

使用vim删除unix中的重复字段(remove duplicate fields in unix with vim)

假设=是在类别行的末尾.... 将文件保存到(例如)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 ...

Vim(7.3.3 Windows 64位):如果没有(^ f = unix),将^ M加到行尾(Vim (7.3.3 Windows 64bit): Add ^M to end of line if there is none (ff=unix))

单个命令可能如下所示:v/^M/s/$/\^M/ 。 这使用<Cv><Cm> ,也就是说它会插入一个文字^M字符,该字符用反斜线转义。 A single command might look like :v/^M/s/$/\^M/. This uses <C-v><C-m>, 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?)

也许你需要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 nee...

如何删除vim Powerline中的段?(How to remove a segment in vim Powerline?)

主题列表表明段名称可能是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])

使用: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 mo...

在vim中缩进[重复](Indenting in vim [duplicate])

首先,对于缩进,有两个>命令,它只是通过添加一个缩进级别向右移动(多少由'shiftwidth'选项确定, 'expandtab'告诉是否使用制表符或空格),并且=命令,适用(特定于语言)缩进规则。 两个命令在加倍时在线上工作,即>> / == ; 通过预先添加数字来解决多行问题。 但他们也采取{motion} ,例如3j (3行向下)或} (到段落的末尾)。 最后,您可以进行视觉选择( V ),使其大小合适,然后将命令应用于它。 First, for indenting there's both...

相关文章

更多

HDFS导出数据到HBase的ROW VALUE设置tricks

在做Hadoop的编程时,有时会用到HBase,常常涉及到把HDFS上面的数据导入到HBase中,在这 ...

Hive 终端产生的问题 (Failed to start database 'metastore_db', see the next exception for details.)

今天使用Hive的时候遇到一个这样的bug,上网查下原来是个小问题,在此记录下。 FAILED: Er ...

HTML5 Web SQL Database 数据库

Web SQL数据库API实际上不是HTML5规范的组成部分,而是单独的规范。它通过一套API来操纵客 ...

《Oracle Database 11g SQL开发指南》(Oracle Database 11g SQL )扫描版[PDF]

中文名: Oracle Database 11g SQL开发指南 原名: Oracle Data ...

myEclipse8.5 New DataBase Connection Driver时出错

大侠看看,菜鸟求教。 这个密码指的是什么密码? 我把可能的秘密输出后有弹出这样的框, 问题补充 ...

《Oracle Database 11g DBA手册》(Oracle Database 11g DBA Handbook )扫描版[PDF]

中文名: Oracle Database 11g DBA手册 原名: Oracle Databa ...

《数据库系统基础:初级篇(第5版)》(Fundamentals of Database Systems (5th Edition))扫描版[PDF]

中文名: 数据库系统基础:初级篇(第5版) 原名: Fundamentals of Databa ...

《数据库系统基础:高级篇(第5版)》(Fundamentals of Database Systems (5th Edition) )扫描版[PDF]

中文名: 数据库系统基础:高级篇(第5版) 原名: Fundamentals of Databa ...

《Oracle Database 10g DBA手册-管理健壮的、可扩展的、高可用的Oracle数据》(Oracle Database 10g DBA Handbook)扫描版[PDF]

中文名: Oracle Database 10g DBA手册-管理健壮的、可扩展的、高可用的Oracl ...

POI读取数据库数据到excel

让我们假设数据表是 emp_tbl 存有雇员信息是从MySQL数据库 test 中检索

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本