Java and currval

Posted on 2005-05-02
Last Modified: 2008-03-17
I'm having trouble getting the currval function to work from a Java program (or from Postgres at all). I want to get the value of addressid in the table taddress. The best I seem to be able to come up with is 'SELECT currval(taddress.addressid)'. However this gives me the error 'ERROR:  relation "1" does not exist' or whatever the number should be. I've tried lots of variations but can't seem to work it out. What am I doing wrong? Thanks.

Question by:samblake
    LVL 3

    Accepted Solution

    I think it's currval('taddress.addressid')
    notice you need to place your sequence in quotes.
    LVL 22

    Expert Comment

    by:earth man2
    import java.lang.*;
    import java.sql.*;

      class PostgreSQLTest {
        public static void main (String[] args) {
          try {
            String     url = "jdbc:postgresql://localhost:5432/YOUR_DATABASE";
            Connection con = DriverManager.getConnection(url, "myaccount", "mypassword");
            Statement  stm = con.createStatement();

            ResultSet  rs  = stm.executeQuery("select addressid from taddress");
          catch (SQLException e) {

    export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/jre/lib/ext/postgresql.jar
    java -Djdbc.drivers=org.postgresql.Driver PostgreSQLTest

    Hello Postgres Java World !
    LVL 22

    Assisted Solution

    by:earth man2
    Maybe you want to get at the value that Postgresql assigned to a column called addressid in table taddress.

    dev=> create table taddress( addressid serial, primary key ( addressid ) );
    NOTICE:  CREATE TABLE will create implicit sequence "taddress_addressid_seq" for serial column "taddress.addressid"
    NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "taddress_pkey" for table "taddress"
    dev=> insert into taddress values ( default );
    INSERT 25497 1
    dev=> select currval('taddress_addressid_seq');
    (1 row)

    dev=> select * from taddress;
    (1 row)

    Also note currval can only be used if the current connection has called nextval('your_sequence')
    LVL 22

    Expert Comment

    by:earth man2
    absong is correct in his contribution.  Share the points with absong.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: ( This requires some add-o…
    Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now