gpt4 book ai didi

java - 按下按钮时无法保存到数据库

转载 作者:行者123 更新时间:2023-11-29 06:27:04 24 4
gpt4 key购买 nike

我有这样一个面板中的按钮代码:

    try{
Koneksi objKoneksi = new Koneksi();
Connection kon = objKoneksi.bukaKoneksi();
Statement stat = kon.createStatement();
String query = "insert into transaksi(invoiceno,kode,nama,jumlah,harga,total) values('"
+Invoice.getText()+"','"
+Kode.getText()+"','"
+Nama.getText()+"','"
+Jumlah.getText()+"','"
+Harga.getText()+"','"
+Total.getText()+"','";
int rows = stat.executeUpdate(query);

if(rows==1){
JOptionPane.showMessageDialog(this,"Data sudah tersimpan");
kon.close();
}
}
catch(SQLException e){}
}

还有一个连接类:

http://pastebin.com/8vZpw9Ek

和语句类:

http://pastebin.com/VK358WJZ

(很抱歉没有在这里发布代码。我在预格式化文本 block 上遇到了一些问题)

当我按下按钮将数据保存到数据库时,出现了错误:

Connection success:
You are not connected to database
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javafinalproject.Aplikasi.SaveActionPerformed(Aplikasi.java:184)
at javafinalproject.Aplikasi.access$200(Aplikasi.java:15)
at javafinalproject.Aplikasi$3.actionPerformed(Aplikasi.java:83)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)dd

还有很多

有人可以帮我吗?先谢谢

最佳答案

这里发生异常:

catch (Exception ex) {
System.out.println("You are not connected to database");
return null;
}

你正在抑制它,你应该通过 ex.printStackTrace() 显示它。无论如何,该方法随后返回 null,这会在此处导致 NullPointerException:

Statement stat = kon.createStatement();
// --------------^^^

更新:

放在这里:

catch (Exception ex) {
System.out.println("You are not connected to database");
ex.printStackTrace();
return null;
}

PS:你最好的办法是去和你的老师/教授谈谈,看来你缺乏解决这个问题所需的基本知识。

关于java - 按下按钮时无法保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com