Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 2nd, 2003, 01:41 PM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
Arrow how to get a result with JDBC that's not a column

Hey there .

What i'm trying to do is to get the result of

select currval('some_sequence_id') with JDBC however
rs.next();
rs.getInt("currval");
throws a wierd exception : postgresql.stat.result

any ideeas ?
__________________
FreeBSD , dooing more with less since 10 years ago

Reply With Quote
  #2  
Old June 2nd, 2003, 08:29 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Hmm, I assume currval is some built in database function? Try using an alias for the function. Something like:

select currval('some_sequence_id') as val




Then you can use "val" as the column name:

rs.getInt("val");

Barring that, use the getInt(int) for an index instead of the column name.

Reply With Quote
  #3  
Old June 3rd, 2003, 12:29 PM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
that works for the built in function . However I also needed this , and I thought it would work the same but it doesn't :

select count(*) from tablename;

Reply With Quote
  #4  
Old June 3rd, 2003, 12:54 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
You can still use the alias

select count(*) as c from tablename;

Reply With Quote
  #5  
Old June 3rd, 2003, 12:58 PM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
duh , i was writting :
select count(*) from contacts as c;

it's hard to be a noob sometimes

thanks again for putting up with some of my less inteligent problems

Reply With Quote
  #6  
Old June 4th, 2003, 08:44 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
heh , I just put this into code now :
Code:
ResultSet rs = stmt.executeQuery("Select count(*) as c from table");
            rs.next();
            currval = rs.getInt("c");

The SQL exception that was thrown by this code is : postgresql.stat.result .
So how can I get that result into JDBC . This is the same problem Exception I was thrown before when i was not using aliasing ...

Reply With Quote
  #7  
Old June 4th, 2003, 09:09 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Hmm, I don't know. besides checking to make sure there IS a next, I don't see anything wrong.
Code:
ResultSet rs = stmt.executeQuery("Select count(*) as c from table");
if(rs.next())
            currval = rs.getInt("c");

Can you get a command prompt for postgresql and type that query in manually? This will remove java/the web server as a potential source of the problem.

The mailing list archives on the postgresql site are down. I would search in there for some anwers too.

Reply With Quote
  #8  
Old June 4th, 2003, 09:17 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
yea , I was playing with those commands on the pg terminal before coding . I allwais tend to run the queries in the terminal at first . They work just fine in the terminal . The same exception was thrown when using 3 different ways of extracting data :
rs.getInt("c"); < - alias
rs.getInt("count"); < - what postgres shows as a "column name" for the result
rs.getInt(1); < - column id

all of them trew the same exception . I don't think this is a postgreSQL fault ... hmmm , I wonder if it could be the driver , but i doubt it ...

are you saying that this is normally the correct JDBC syntax ?

Reply With Quote
  #9  
Old June 4th, 2003, 09:21 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,808 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 21 h 29 m 33 sec
Reputation Power: 278
This is what I'm using (on MySQL, but I don't think that the problem is in the db):

<%@ page language="java" import="java.sql.*" %>
<%
// define variables
String Count;

// define database parameters
String host="localhost";
String user="tbd";
String pass="tbd";
String db="db";
String conn;
%>
<%

Class.forName("org.gjt.mm.mysql.Driver");

// create connection string
conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" + pass;

// pass database parameters to JDBC driver
Connection Conn = DriverManager.getConnection(conn);

// query statement
Statement SQLStatement = Conn.createStatement();

// generate query
String Query = "select count(*) as c from table";

// get result
ResultSet SQLResult = SQLStatement.executeQuery(Query);

while(SQLResult.next())
{
Count = SQLResult.getString("c");
out.println("<tr><td>" + Count + "</td></tr>");
}

// close connection
SQLResult.close();
SQLStatement.close();
Conn.close();

%>

Reply With Quote
  #10  
Old June 4th, 2003, 09:30 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
heh ...
thanks pabloj ... the problem was that I was trying to retreive an integer and I was getting a String of some sort . I noticed you were using strings in your code . So I tried that and it worked ...

I find it a bit odd that u'd want to retreive strings for things as :

count() or max() or sum() , but at least it's not working ...

thanks once again to u guys , for offering me very good help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > how to get a result with JDBC that's not a column


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway