Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2004

Tip: Looking for answers? Try searching our database.

java, mysql autoincrememnt

Thread view: 
Dan - 20 Apr 2004 23:25 GMT
I am writing a program in java which at some point will insert values
into a mysql table whose primary key auto increments. I need to know
what the value that is used for this new row is.

Does any one have any ideas of how to find this out without having to
search the table imediately after the insertion to find the value that
is has given the new row?

any help would be fantastic
Thanks
Dan
Mark Hansen - 20 Apr 2004 23:44 GMT
> I am writing a program in java which at some point will insert values
> into a mysql table whose primary key auto increments. I need to know
[quoted text clipped - 7 lines]
> Thanks
> Dan

If you're using Java 1.4, have a look at the getGeneratedKeys() method on
the Statement object.
Dan - 21 Apr 2004 00:51 GMT
Thankyou, that has been useful, only think is I can't get it to work!
my code works fine with
...
stmt.executeUpdate(query);
...

but as soon as I change it to

...
stmt.executeUpdate(query, java.sql.Statement.RETURN_GENERATED_KEYS);
...

it compiles fine, but gives this error when the Class it is in is called
from another class

Exception in thread "main" java.lang.AbstractMethodError:
org.gjt.mm.mysql.Statement.executeUpdate(Ljava/lang/String;I)I
        at SWE.DatabaseExternal.getStatement(DatabaseExternal.java:26)
        at Test.main(Test.java:6)

any idea why?
Thanks
Dan

>> I am writing a program in java which at some point will insert values
>> into a mysql table whose primary key auto increments. I need to know
[quoted text clipped - 10 lines]
> If you're using Java 1.4, have a look at the getGeneratedKeys() method on
> the Statement object.
David Harper - 21 Apr 2004 09:05 GMT
> Thankyou, that has been useful, only think is I can't get it to work!
> my code works fine with
[quoted text clipped - 17 lines]
>
> any idea why?

It looks like you're using an out-of-date version of the MySQL JDBC driver
which doesn't have the necessary JDBC 3 methods.

Try downloading the latest stable version of Connector/J from www.mysql.com
and re-run your program with the new driver. Also, make sure that you're
running under Java 1.4.

David Harper
Cambridge, England
Dan - 21 Apr 2004 16:36 GMT
yes you are right, discovered that that is the problem today. The
problem is tho that I am not using my own computer to program on, I am
using the university sun server, and the computer I am useing only has
JDBC 1.1 and JDK 1.2 for tomcat. Does any one know who to solve my
problem using these out dated methods?

Thanks Dan

>>Thankyou, that has been useful, only think is I can't get it to work!
>>my code works fine with
[quoted text clipped - 27 lines]
> David Harper
> Cambridge, England
David Harper - 21 Apr 2004 20:28 GMT
> The problem is tho that I am not using my own computer to program on,
> I am using the university sun server, and the computer I am useing
> only has JDBC 1.1 and JDK 1.2 for tomcat. Does any one know who to
> solve my problem using these out dated methods?
[SNIP]

You should be able to download JDK 1.4 from the Sun Java web site and
install it locally e.g. in your home directory, without the need for
superuser privileges. I've installed Java in this way on several Unix
platforms when I needed to use a more recent version than the one that
my sysadmin had installed centrally.

Then all you need to do is adjust your PATH environment variable by
prepending the location of the JDK 1.4 binary directory, and you're up
and running with Java 1.4.

That automatically gives you JDBC 3 capabilities, provided that your
JDBC driver supports them.

Hope this helps.

David Harper
Cambridge, England
Dan - 21 Apr 2004 21:33 GMT
Thats the funny thing, because I have put my own mysql JDBC driver in my
classes directory, and JDK 1.4 is installed, so normal java programs run
fine. But as soon as I change the java file into a JSP page, which is
run under tomcat (which is using java 1.2), the page shows an error
basically saying that it doesn;t support the getGeneratedKeys() method.

Do I need to change the tomcat environment variables or something?

>> The problem is tho that I am not using my own computer to program on,
>> I am using the university sun server, and the computer I am useing
[quoted text clipped - 20 lines]
> David Harper
> Cambridge, England
David Harper - 22 Apr 2004 08:37 GMT
> Thats the funny thing, because I have put my own mysql JDBC driver in my
> classes directory, and JDK 1.4 is installed, so normal java programs run
[quoted text clipped - 3 lines]
>
> Do I need to change the tomcat environment variables or something?

Yes, you do.

You need to set JAVA_HOME to point to the location of the Java
installation that you wish to use.

It's defined in the startup shell script in your Tomcat distribution, so
you have to edit the startup script to set the correct value of
JAVA_HOME, then re-start your Tomcat server.

David Harper
Cambridge, England
Sergey Stepanenko - 21 Apr 2004 23:07 GMT
> yes you are right, discovered that that is the problem today. The
> problem is tho that I am not using my own computer to program on, I am
> using the university sun server, and the computer I am useing only has
> JDBC 1.1 and JDK 1.2 for tomcat. Does any one know who to solve my
> problem using these out dated methods?

You may use MySQL-specific query to get value of the autoincrement field:
SELECT LAST_INSERT_ID()
--
Dan - 22 Apr 2004 15:15 GMT
Thank you every one, decided to go with SELECT LAST_INSERT_ID() since it
would mean less fiddling witht he server.
Cheers for the help
Dan

>> yes you are right, discovered that that is the problem today. The
>> problem is tho that I am not using my own computer to program on, I am
[quoted text clipped - 4 lines]
> You may use MySQL-specific query to get value of the autoincrement field:
> SELECT LAST_INSERT_ID()
Roedy Green - 21 Apr 2004 00:34 GMT
>I am writing a program in java which at some point will insert values
>into a mysql table whose primary key auto increments. I need to know
>what the value that is used for this new row is.

Let SQL handle it. Here is how I did it in SYBASE.

CREATE TABLE people(
       acct            integer  NOT NULL DEFAULT AUTOINCREMENT,

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.