Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java and currval

Posted on 2005-05-02
7
Medium Priority
?
554 Views
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.

-Sam
0
Comment
Question by:samblake
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
absong earned 100 total points
ID: 13912325
I think it's currval('taddress.addressid')
notice you need to place your sequence in quotes.
0
 
LVL 22

Expert Comment

by:earth man2
ID: 13913641
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();

        stm.setQueryTimeout(10);
        ResultSet  rs  = stm.executeQuery("select addressid from taddress");
        rs.next();
        System.out.println(rs.getString(1));
      }
      catch (SQLException e) {
        System.out.println("Exception!");
        System.out.println(e.toString());
    }
  }
}

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

Hello Postgres Java World !
0
 
LVL 22

Assisted Solution

by:earth man2
earth man2 earned 100 total points
ID: 13913677
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"
CREATE TABLE
dev=> insert into taddress values ( default );
INSERT 25497 1
dev=> select currval('taddress_addressid_seq');
 currval
---------
       1
(1 row)

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

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

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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: http://www.postgresql.org/ (http://www.postgresql.org/) 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.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

581 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