aspose file tools*
The moose likes JDBC and the fly likes Putting Database values in JavaBeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Putting Database values in JavaBeans" Watch "Putting Database values in JavaBeans" New topic
Author

Putting Database values in JavaBeans

Sunil Chandurkar
Ranch Hand

Joined: Jan 09, 2008
Posts: 37
I have a table called counter in mysql.
One field called count datatype integer.

Here's the class:
<code>
public String execute(){
private int number;

public void setNumber...

public int getNumber...

try
{
Class.forName...
Connection con ...
Statement stmt ...
ResultSet rs = stmt.executeQuery(select*from counter);
while (rs.next())
{
number = rs.getInt("count");
}
catch...

}
</code>

What should I add in the code so that the Javabean number property will hold the integer count retrieved from the database.

Thanks
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

I am not sure you have posted complete problem.

Correct me if I am wrong, the counter table has a column named count as integer type then there would be many count count unless you add something in your where clause. so how do you want to hold them in single variable ?

What is your exact problem ?

Shailesh


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Sunil Chandurkar
Ranch Hand

Joined: Jan 09, 2008
Posts: 37
Shailesh,

I have inserted values for only one row. So the query "select * from count"
returns only one row.

Thanks

Sunil
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Ok, so what is the problem you are facing ? any error ?

Shailesh
[ September 16, 2008: Message edited by: Shailesh Chandra ]
mark walter
Greenhorn

Joined: Jun 20, 2008
Posts: 15
Is this what you are trying to do?

<code>
private int number;

public void setNumber...

public int getNumber...

public String execute(){
try
{
Class.forName...
Connection con ...
Statement stmt ...
ResultSet rs = stmt.executeQuery(select*from counter);
while (rs.next())
{
this.setNumber(rs.getInt("count"));
}
catch...

}
</code>
Sunil Chandurkar
Ranch Hand

Joined: Jan 09, 2008
Posts: 37
Thanks...I found the answer. Just had to call the setter method and pass the
rs.getInt("count") as the argument.
 
jQuery in Action, 2nd edition
 
subject: Putting Database values in JavaBeans