gpt4 book ai didi

java - JDBC 空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:54:25 28 4
gpt4 key购买 nike

我在 swing 中编写了一段代码,其中在 jTextFields 中插入了文本。但是当我尝试将它们插入数据库时​​,它在“Driver Connected”之后给出 NullPointerException。以下是我的 JDBC 代码:

    Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try{

Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Driver Loaded.");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "usname", "pass");
System.out.println("Driver Connected.");

String insert = "INSERT INTO directorsdb values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pst=conn.prepareStatement(insert);

pst.setString(1, dirname.getText());
pst.setString(2, jDateChooser1.getDate().toString());
pst.setString(3, DIN.getText());
pst.setString(4, PAN.getText());
pst.setString(5, UIN.getText());
pst.setString(6, birthplace.getText());
pst.setString(7, passportno.getText());
pst.setString(8, dlicense.getText());
pst.setString(9, nationality.getText());
pst.setString(10, occupation.getText());
pst.setString(11, occupationcompany.getText());
pst.setString(12, designation.getText());
pst.setString(13, qualification.getText());
pst.setString(14, officeadd.getText());
pst.setString(15, residentialadd.getText());
pst.setString(16, contacthome.getText());
pst.setString(17, contactwork.getText());
pst.setString(18, contactmobile.getText());
pst.setString(19, emailper.getText());
pst.setString(20, emailwork.getText());
pst.setString(21, emailother.getText());
pst.setString(22, secretaryname.getText());
pst.setString(23, secretarycontact.getText());
pst.setString(24, secretaryemail.getText());
pst.setString(25, buttonGroup1.getSelection().getActionCommand());
pst.setString(26, jDateChooser2.getDate().toString());
pst.setString(27, fathername.getText());
pst.setString(28, mothername.getText());
pst.setString(29, jDateChooser3.getDate().toString());
pst.setString(30, posiionboard.getText());
pst.setString(31, jDateChooser4.getDate().toString());
pst.setString(32, cessationreason.getText());
pst.setString(33, buttonGroup2.getSelection().getActionCommand());
pst.setString(34, buttonGroup3.getSelection().getActionCommand());
pst.setString(35, aboutd.getText());

pst.execute();


}catch(SQLException e){
System.err.println(e.fillInStackTrace());
}
catch(ClassNotFoundException ex){
System.err.println(ex);
} finally{ // != means NOT EQUAL TO
if (rs != null){
try {
rs.close();
} catch (SQLException ex) {
System.err.println(ex);
}

}
if (pst != null){
try {
pst.close();
} catch (SQLException ex) {
System.err.println(ex);
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException ex) {
System.err.println(ex);
}
}
}

以下是异常(exception)情况:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DirectorDB.NewEntry.jButton1ActionPerformed(NewEntry.java:882)
at DirectorDB.NewEntry.access$1800(NewEntry.java:28)
at DirectorDB.NewEntry$19.actionPerformed(NewEntry.java:738)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

What appears at line 882 of NewEntry?

回复...

pst.setString(2, jDateChooser1.getDate().toString())

那么我建议jDateChooser1.getDate()null

您不应该使用String设置日期字段,数据库应该设置为使用适当的日期/时间数据类型,并且您应该尝试使用pst.setDate(.. .) 相反。

这将以易于加载的方式存储日期/时间值。您应该注意 Date#toString 将以当前本地指定的格式存储日期,这使得加载和解析变得困难

如果您无法使用数据库中的实际日期值,那么您应该使用更像...的东西

Date value = jDateChooser1.getDate();
pst.setString(2, value == null ? null : value.toString());

更好的是,使用 SimpleDateFormatString 值格式化为商定的格式

关于java - JDBC 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24005558/

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