I have a project for school I have coded using BlueJ, connecting to a database in sqlite and the visual side being from javafx for a pc application. I wanted to create a search bar that would search for the user ID of existing users. In the existing user controller class I have the following code linked to the search bar.
@FXML void searchReleased(){
User.nameSearch(searchnamefield.getText());
}
And in my model class for the user table in my database I have setup this code regarding the actual function of the search.
public static void nameSearch(String searchString)
{
try
{
PreparedStatement statement = Application.database.newStatement("SELECT FirstName, Surname, UserID FROM User WHERE Firstname LIKE '?%' OR Surname LIKE '?%'");
statement.setInt(1, UserID);
if (statement != null)
{
Application.database.executeUpdate(statement);
}
}
catch (SQLException resultsexception)
{
System.out.println("search result processing error: " + resultsexception.getMessage());
}
}
Despite this it doesn't seem to work, the project compiles however when running and trying the search field, some error comes up in the log. can anyone see what's wrong and how I would test this working. Thanks.
Here's the error log
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:197)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:147)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$353(GlassViewEventHandler.java:228)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:227)
at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
at com.sun.glass.ui.View.notifyKey(View.java:966)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 26 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:110)
at org.sqlite.jdbc3.JDBC3PreparedStatement.setInt(JDBC3PreparedStatement.java:291)
at User.nameSearch(User.java:43)
at ExistingUserScene.searchReleased(ExistingUserScene.java:147)
... 36 more
The immediate cause of the exception is that your parameter markers (
?
) in the SQL are inside quotes, so they are not interpreted as parameter markers at all. (The SQL is going to result in a search for names that literally begin with?
.) Consequently when you callstatement.setInt(1, ...)
, it is looking for the first parameter marker: when it finds none it throws anArrayIndexOutOfBoundsException
. You probably intended something likeand then you would do
Note that there are many more things wrong here, for example
executeUpdate(...)
to execute a queryand maybe many others I haven't seen. So this change won't get rid of all the exceptions or make it work the way you want it, but it will fix the immediate exception about which you asked.