aspose file tools*
The moose likes JDBC and the fly likes how to get integer from table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to get integer from table" Watch "how to get integer from table" New topic
Author

how to get integer from table

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
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

Joined: Apr 14, 2004
Posts: 10336


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

What did you try? What went wrong?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Jun 03, 2008
Posts: 17
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

Joined: May 29, 2008
Posts: 268
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

Joined: Apr 14, 2004
Posts: 10336

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get integer from table
 
Similar Threads
question reg. a stored procedure
table cell cut paste
Javascript help needed?
Compiling errata for K&B, SCJP 6
Hashtable in memory versus database table in memory