aspose file tools*
The moose likes Beginning Java and the fly likes Help me get started Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help me get started" Watch "Help me get started" New topic
Author

Help me get started

Josey Wales
Greenhorn

Joined: Jan 30, 2011
Posts: 5

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?


“I tried being reasonable, I didn't like it.”
Clint Eastwood
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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:



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Josey Wales
Greenhorn

Joined: Jan 30, 2011
Posts: 5

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

Joined: Aug 16, 2005
Posts: 14266
    
  21

Here's a good tutorial for learning how to use JDBC: Oracle's JDBC Tutorial
Josey Wales
Greenhorn

Joined: Jan 30, 2011
Posts: 5

Thanks again Jesper, I appreciate it!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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

Joined: Jan 30, 2011
Posts: 5

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

Joined: Aug 16, 2005
Posts: 14266
    
  21

Oh, it should be rs.next(). Sorry!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Jan 30, 2011
Posts: 5

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

Joined: Mar 28, 2003
Posts: 11476
    
  94

Please check your private messages regarding an important administrative matter.

-Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me get started