我想获得实际创建的主键。 我需要立即为另一个方法,但它返回一个错误。 但它返回一个SQLE。我不知道我的错误。 我希望我能给你一些信息。 (System.out.println(id)仅供我检查它是否返回正确的PrimaryKey)
CREATETABLE:
CREATE TABLE "MitarbeiterInfo" ("Vorname" TEXT, "Nachname" TEXT, "Geburtsdatum" CHAR, "Wohnadresse" TEXT, "Postleitzahl" TEXT, "Eintrittsdatum" CHAR, "Handynummer" TEXT, "Email" TEXT, "ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL )
GuiClass:
public void actionPerformed(ActionEvent e) {
new Datenbank().mitarbeiterHinzufügen(
textField.getText(),textField_1.getText(),textField_2.getText(),textField_3.getText(),
textField_4.getText(),textField_5.getText(),textField_6.getText(),textField_7.getText());
refreshTable();
}
DatabaseClass:
public void mitarbeiterHinzufügen(String v, String n, String g, String w, String p, String e, String h, String email){
conn=Datenbank.dbConnector();
try {
String query="insert into MitarbeiterInfo (Vorname,Nachname,Geburtsdatum,Wohnadresse,Postleitzahl,Eintrittsdatum,Handynummer,Email,ID) values (?,?,?,?,?,?,?,?,?)";
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1, v);
pst.setString(2, n);
pst.setString(3, g);
pst.setString(4, w);
pst.setString(5, p);
pst.setString(6, e);
pst.setString(7, h);
pst.setString(8, email);
pst.execute();
String identitiy = "SELECT IDENTITY_VAL_LOCAL() FROM MitarbeiterInfo";
ResultSet rs = pst.executeQuery(identitiy);
rs.next();
int id = rs.getInt("1");
System.out.println(id);
JOptionPane.showMessageDialog(null, "Mitarbeiter hinzugefügt");
pst.close();
} catch (Exception b) {
b.printStackTrace();
}
}
那个错误发生了:
java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.Unused.unused(Unused.java:31)
at org.sqlite.PrepStmt.executeQuery(PrepStmt.java:596)
***at Datenbank.mitarbeiterHinzufügen(Datenbank.java:69)***
at GUI$3.actionPerformed(GUI.java:138)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
在java.sql.ResultSet中,下面的方法是为getInt:
定义的getInt(int columnIndex)
getInt(String columnLabel)
我在您的代码中发现以下行的问题:
int id = rs.getInt("1");
你应该通过其中一个选项获得价值:
int id = rs.getInt(1);
或者
int id = rs.getInt("Column_name");
答案 1 :(得分:0)
尝试
SELECT last_insert_rowid() FROM MitarbeiterInfo
代替。
您获得的例外情况似乎暗示SQLite不支持IDENTITY_VAL_LOCAL()
。