Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Tolls

Thread: Using variables in query

  1. #1
    drimades is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Using variables in query

    I'm a jdbc connection to a db and I need to customize the query. I'm using the following to find the records with id=code, where code is an integer variable and so is the field id.

    Java Code:
    String SQL1 = "SELECT * from app.tax WHERE id=" + Integer.toString(code);
    It doesnt work. Is it a syntax error? What is the right form?

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Using variables in query

    It doesnt work
    Define "it doesn't work".

  3. #3
    drimades is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Using variables in query

    it prompts some errors:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    etc.
    and the query doesnt return the desired result

  4. #4
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Using variables in query

    That is not the full exception stack trace....post it all - and within it is essential information which tells you what line of code the exception is being thrown on, so it helps to post that relevant code.

  5. #5
    drimades is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Using variables in query

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:493)
    	at java.lang.Integer.parseInt(Integer.java:514)
    	at himaratest.NewJFrame2.jButton1ActionPerformed(NewJFrame2.java:331)
    	at himaratest.NewJFrame2.access$000(NewJFrame2.java:13)
    	at himaratest.NewJFrame2$1.actionPerformed(NewJFrame2.java:89)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    	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:6268)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6033)
    	at java.awt.Container.processEvent(Container.java:2045)
    	at java.awt.Component.dispatchEventImpl(Component.java:4629)
    	at java.awt.Container.dispatchEventImpl(Container.java:2103)
    	at java.awt.Component.dispatchEvent(Component.java:4455)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
    	at java.awt.Container.dispatchEventImpl(Container.java:2089)
    	at java.awt.Window.dispatchEventImpl(Window.java:2517)
    	at java.awt.Component.dispatchEvent(Component.java:4455)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    	at java.awt.EventQueue.access$000(EventQueue.java:96)
    	at java.awt.EventQueue$1.run(EventQueue.java:608)
    	at java.awt.EventQueue$1.run(EventQueue.java:606)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    	at java.awt.EventQueue$2.run(EventQueue.java:622)
    	at java.awt.EventQueue$2.run(EventQueue.java:620)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

  6. #6
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Using variables in query

    So...what is on line 331 of your NewJFrame2 class? I presume an Integer.parseInt call....see the API for this method, and reasons why it may throw a NumberFormatException (hint: you are trying to parse an empty String into a int)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Using variables in query

    That exception aside (which as doWhile has pointed out is not in the line of code you have supplied), taking just the line you have given...you should be using a PreparedStatement.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. need sql query
    By murali23krishna in forum JDBC
    Replies: 8
    Last Post: 05-17-2011, 11:42 AM
  2. SQL query to HQL
    By alvin0618 in forum JDBC
    Replies: 3
    Last Post: 04-08-2011, 09:00 AM
  3. hql query
    By murali23krishna in forum JDBC
    Replies: 1
    Last Post: 03-17-2011, 07:25 AM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. I need help with a query.
    By Daredemo in forum JDBC
    Replies: 1
    Last Post: 08-13-2008, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •