Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me get started

 
Josey Wales
Greenhorn
Posts: 5
Firefox Browser Mac OS X Objective C
  • Mark post as helpful
  • send pies
  • 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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Mac OS X Objective C
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Mac OS X Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Jesper, I appreciate it!
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Mac OS X Objective C
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, it should be rs.next(). Sorry!
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Mac OS X Objective C
  • Mark post as helpful
  • send pies
  • 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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11877
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check your private messages regarding an important administrative matter.

-Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic