• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Help me get started

 
Greenhorn
Posts: 5
Mac OS X Firefox Browser Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone. Believe it or not, I've been fighting with my simple little java program all day long and cannot figure this out and java tutorials don't seem to help. I'm just getting started with java and mysql so I know next to nothing. I'm using netbeans 6.9.1 to create my java GUI program. I have created and successfully run a '.sql' file. I have two tables and have initialized the database with one record in one of the tables. I can see the data from netbeans IDE services so I know the data is there. In my program, I can successfully open and connect to my database. The problem is, I don't know how to retrieve a specific record from the table.

Here's a code snippet where I attempt to get a specific record:



'numID' is an integer variable previously filled. 'accountID' is an integer field in 'Customers' table. First of all, is this query correct? If it is, how do I know that a record with accountID equal to numID was found and how do I read the data?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I guess you want to do a query which looks for a record with a specific number. The syntax with @numID is not going to work. Is numID a variable in your program? Where did you learn that syntax?

The best way to do queries that require parameters is by using a PreparedStatement, for example:

 
Josey Wales
Greenhorn
Posts: 5
Mac OS X Firefox Browser Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes numID is a variable. I got the '@' syntax from a website that was showing examples. I will try the example you gave and thanks a lot. Forums always seem to help more than any other source.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a good tutorial for learning how to use JDBC: Oracle's JDBC Tutorial
 
Josey Wales
Greenhorn
Posts: 5
Mac OS X Firefox Browser Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Jesper, I appreciate it!
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:I guess you want to do a query which looks for a record with a specific number. The syntax with @numID is not going to work. Is numID a variable in your program? Where did you learn that syntax?


It's stored procedure syntax. You use @ for variables in stored procedures.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, ok. But you cannot use that to refer to variables in your Java source code, which is what Josey tried to do (if I understood it correctly).
 
Josey Wales
Greenhorn
Posts: 5
Mac OS X Firefox Browser Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Welcome to the Ranch.

I guess you want to do a query which looks for a record with a specific number. The syntax with @numID is not going to work. Is numID a variable in your program? Where did you learn that syntax?

The best way to do queries that require parameters is by using a PreparedStatement, for example:



This all works except the rs.hasNext(). There doesn't seem to be a hasNext() method and I don't see anything I can substitute.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, it should be rs.next(). Sorry!
 
Rob Spoor
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's next() you should use. This goes to the next record (to the first if called for the first time) and returns true, or does nothing and returns false if you've already went through all records. Usually you call next() in a loop, but if you know there's going to be only zero or one record then if is also good.
 
Josey Wales
Greenhorn
Posts: 5
Mac OS X Firefox Browser Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That did it! Thanks to both of you. I have a good start now and a url to a good tutorial.
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check your private messages regarding an important administrative matter.

-Andrew
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic