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

Putting Database values in JavaBeans

 
Sunil Chandurkar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sunil Chandurkar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so what is the problem you are facing ? any error ?

Shailesh
[ September 16, 2008: Message edited by: Shailesh Chandra ]
 
mark walter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...I found the answer. Just had to call the setter method and pass the
rs.getInt("count") as the argument.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic