Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PostgreSQL jdbc driver Problem

 
Raghavendra Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
i have tried the following code snippet.....(i am giving the required part only...)
preparedStatement = connection.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
resultSet = preparedStatement.getGeneratedKeys();
Integer primaryKey = null;
if(resultSet.next()) {
primaryKey = new Integer(resultSet.getInt(1));
}
At Runtime i am getting the following message
"This method is not yet implemented."
i have used the postresql jdbc driver(i.e pg73jdbc3.jar in my classpath)
so how to proceed can any one please help me.....
Thanks in Advance
Raghu
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't told us which line is throwing the error, but I'm guessing it's either prepareStatement(query,Statement.RETURN_GENERATED_KEYS) or getGeneratedKeys()
Many of the things you interact with in JDBC are Interfaces and not Classes. The Driver provider is responsible for implementing these Interfaces. This means they have to provide code for all of the methos in the Interface.... But... sometimes they don't. They just cheat and provide an empty method that throws an Exception saying "Oops, I haven't gotten up to this yet".
It looks like the functionality you are looking for doesn't exist in your current Driver.
Dave.
 
Raghavendra Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank U
i also expected the same reason behind it.i.e no implementation in the driver.
but how to solve my problem regarding postrgresql database....
please help me..
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra,
After the data is inserted, you need to write your own queries to get they keys. If the data has any unique values, you could base it on that. Otherwise, you could see which keys are there that were not before.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never used this feature, but it appears that this is a mechanism for allowing the Driver to manage (or interact with the database as it manages) keys. So if the Driver won't manage it, you are left with two options:
1) let the Database manage keys
2) do it yourself.
Option 1 is like what Jeanne was describing. You write the query including some database specific code that gets the dtabase to insert the next generated value as the statement is inserted. I tend not to use this since the queries are database specific and I've never trusted the thread safety - how do you guarentee that the value you read back is the one you wrote and not a record someone else wrote? Without the primary key it's a bit disconnected.
Option involves getting the key from an external key generator before you insert the data into the database. This is the way I tend to use it for several reasons. Firstly, you already have the primary key so you don't have to go looking for it. Secondly, the mechanism for generating the key can be 'pluggable' ie you can have multiple key generation strategies and plug in the one you need for this particular application. It may be one of several database-specific key generation strategies, an incrementing counter, or some fancy distributed key generator. It doesn't really matter. Yes, you may need two database operations to get the next key value then insert the record, but you'd be doing that if you had to load the key in a second select operation.
Just my thoughts.
Dave.
 
Raghavendra Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You One and All
 
Raghavendra Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! all
i have used the following method after inserting the record to retrieve the primarykey.please verify and tell me whether this will give the exact primary key for the record i have inserted...i am giving the database field description also after this method..
public static int getLastPrimaryKey(String tableName, String pkFieldName)
{
int key = -1;
String query = "SELECT MAX(" + pkFieldName + ") FROM " + tableName;
Statement stmt = null;
ResultSet rs = null;
Connection connection = SQLUtil.getConnection();
try
{
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
rs.next();
key = rs.getInt(1);
return key;
}
catch (Exception ex)
{
return -1;
}
finally
{
//rs = SQLUtil.closeResultSet(rs);
//stmt = SQLUtil.closeStatement(stmt);
if (connection != null)
{
connection = null;
}
}
}

postgres Database
------------------
message_cod | bigint | not null default nextval('messages.b2l_message_sequence'::text)
Thanks in advance
Raghavendra Rao
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra,
Assuming you only have one user at a time and the keys are generated in ascending order, this works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic