Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get integer from table

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a doubt. Suppose there is a column which consists of Int data (let us take as 7). Now i want this number from table directly in the program. I have tried doing with ResultSet and PreparedStatement but i failed. I should be able to assign it to a variable," example it should look like num = 7". Now How Can I Achieve This. Please Help!
Thanks & Regards.
[ June 10, 2008: Message edited by: Bear Bibeault ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have tried doing with ResultSet and PreparedStatement but i failed.

What did you try? What went wrong?
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually tried to count the number of rows in a tablet to get the value.
like this

if(ae.getActionCommand()=="Add")
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
conn = DriverManager.getConnection("jdbcdbc:addissue","library", "adeeb");
stmt
=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * FROM addissue");
rs.last();
int num = rs.getRow();
System.out.println("Number of Rows = "+num);
}
catch(Exception e)
{
System.out.println("Error " + e);
}

Now i wanted to get the number of rows of particular value in a column using the same Syntax but only changed query like this

"rs = stmt.executeQuery("SELECT COUNT(*) FROM addissue WHERE rollno = 1201");"

now i get the answer i.e num = 1, because this query returns only one row. I know that, but the actual count of rows is different

Now are you clear.
Thanks for Replying My Question.

[ June 10, 2008: Message edited by: adeeb alexander ]
[ June 10, 2008: Message edited by: adeeb alexander ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run your query:

in a SQL client, how many rows are returned?

(NB: why are you using the JDBC-ODBC bridge? Is it Access you are using?)
[ June 10, 2008: Message edited by: Paul Sturrock ]
 
akhilesh pandey
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can check the javadocs, the rs.getRow() method returns the current row number.Say when you execute your query "SELECT COUNT(*) FROM addissue WHERE rollno = 1201" the OUTPUT will be 7.So there is only one row in the output so you are getting the result as one.I am not pretty sure but have you tried with rs.getInt(1).I think it should work.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
paul can you please say me why i should not use jdbcdbc drivers is there any prblems in future please give me your suggestion
Thanks for answering my questions and more over the problem is solved now by using rs.getInt(colNum)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the biggest issues is it doesn't support concurrent connections. On a normal RDBMS this would be introducing a significant bottleneck.

Aside from this, from Sun's own documentation:

The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

As far as I am aware the only database that does not have a JDBC driver is Access. Other than that a proper vendor provided driver is far better. Trust me, swapping to a proper driver will save you various headaches in the future!
[ June 10, 2008: Message edited by: Paul Sturrock ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic