Hallo,
Ich verwende prepared statements um datensätze in eine datenbank einzufügen. ich will zuletzt eingefügte datensätze (die über auto inkrement eine id bekommen) zurückgeliefert bekommen. Ich habe dabei die methode getGeneratedKeys() verwendet (hab den relevanten Teil vom Code gepostet, der Probleme macht)
package DatabaseHandling.DAO;
import java.sql.*;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import pmdb.databasehandling.dao.IActorManager;
import DatabaseHandling.ConnectionManager;
import WebLookUp.Filter;
import Entities.Actor;
import Entities.Movie;
import Entities.Person;
public class ActorManager implements IActorManager
{
PreparedStatement insertStatement;
PreparedStatement updateStatement;
PreparedStatement deleteStatement;
PreparedStatement selectStatement;
public ActorManager() throws ClassNotFoundException, SQLException {
Connection con = ConnectionManager.getConnection();
insertStatement = con
.prepareStatement("insert into person (first_name, last_name, birthdate, place_of_birth) values(?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS );
updateStatement = con
.prepareStatement("update person set first_name = ?, last_name = ?, birthdate = ?, place_of_birth = ? where id = ?");
deleteStatement = con
.prepareStatement("delete from person where id = ?");
selectStatement = con.prepareStatement("select * from person");
}
@Override
public Integer saveActor(Actor actor) throws DBException
{
String sFirstName = actor.getFirstName();
String sLastName = actor.getLastName();
Date dBirthDate = (Date) actor.getBirthDate();
String sPlaceOfBirth = actor.getPlaceOfBirth();
try {
insertStatement.setString(1, sFirstName);
insertStatement.setString(2, sLastName);
insertStatement.setDate(3, dBirthDate);
insertStatement.setString(4, sPlaceOfBirth);
insertStatement.executeUpdate();
ResultSet keys = insertStatement.getGeneratedKeys();
keys.next();
int key = keys.getInt(1);
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
Alles anzeigen
Ich bekomme folgenden Fehler:
ava.sql.SQLException: This function is not supported
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.notSupported(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at DatabaseHandling.DAO.ActorManager.<init>(ActorManager.java:40)
at Controls.AddCastWindow.actionPerformed(AddCastWindow.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
FirstName: asa
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Controls.AddCastWindow.actionPerformed(AddCastWindow.java:128)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Weiß jemand was da nicht passt? Bin schon ziemlich lang am suchen