This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Auto Increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Auto Increment" Watch "Auto Increment" New topic

Auto Increment

Raymond O'Leary

Joined: Mar 23, 2004
Posts: 24
Hey There,
I've put an auto increment into my database for updating some of my tables.
When using preparedStatements I know that you dont put in a question mark in the insert statement, but what does one put in there?
This is the code and assume that the auto incfrement is the first field in the table.
String query = "INSERT INTO SUPERLOG VALUES (?,?,?,?,?,?,?)";
And when doing the -
-stuff do you leave one blank? Or do you leave it out altogether?
Here is the code...

String query = "INSERT INTO SUPERLOG VALUES (?,?,?,?,?,?,?)";
PreparedStatement Stmt = con.prepareStatement(query);
String UName = (String) session.getAttribute("username");
Thanks guys.
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
You can specify the fields to set, excluding the auto-increment one.
sql = "INSERT INTO tablename(someField, someOtherField, ...) VALUES(?,?,...)"
Raymond O'Leary

Joined: Mar 23, 2004
Posts: 24
Ok this is what I've got but it still dont work, can anybody shed any light on this? I'm just unsure of the format of some of the statements.

<%@ page language="java" contentType="text/html"
import="java.util.*" import="oracle.jdbc.*" import="java.sql.*"%>
<body bgColor="WHITE" text = "MIDNIGHTBLUE">
<img src="witlogo.gif" align=left><H2><font color=midnightblue>X</font></H2><hr>
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc rawit","X", "X");
String query = "INSERT INTO SUPERLOG VALUES ('',?,?,?,?,?,?)";
PreparedStatement Stmt = con.prepareStatement(query);

String UName = (String) session.getAttribute("username");
<p> <br><p> <br><p> <H3><b>Your Comment Was Successfully Added</b></h3>
Thanks Lads
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140
You need to try what BillyBob Marshall suggests.

needs to become:

where <fieldname> is a name of a field in your table.
[ March 26, 2004: Message edited by: eammon bannon ]
Raymond O'Leary

Joined: Mar 23, 2004
Posts: 24
Hey there,
So this will work? Cant get the bloody thing to run.
if ( ( request.getParameter("submit")).equals("submit") )
Statement statement1 = con.createStatement();
String sup=(String) session.getAttribute("username");
String grp = request.getParameter("t1");
String day = request.getParameter("D2");
String month = request.getParameter("D3");
String year = request.getParameter("D4");
String com = request.getParameter("comment");
String strSQL = "INSERT INTO SUPERLOG VALUES(NEXTSUPERLOG.nextval , "+" '"+sup+"' , "+" '"+grp+ "' , "+" '"+ day+"' , " + " '" +month +"' , "+" '"+year+"'," + "'" +com +"')";
String strSQL1 = "commit";
ResultSet rs1 = statement1.executeQuery(strSQL);
Thanks for the help.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Can't see the forest for the trees as the saying goes.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

If you want us to properly help you, then you need to supply some more information.

Can't get the bloody thing to run.

Then posting the entire error message and stack trace you are getting would aid us greatly in our quest to help you overcome your problem.
It appears that you are using an Oracle database, and that you have defined an Oracle "sequence" object -- namely NEXTSUPERLOG, so yes, in order to get the next (sequence) value, you need to use:

However, it appears that you are not taking into consideration the data types of the columns in your SUPERLOG table. From what I can gather from your posts, it would appear that this table has the following columns:
  • ID
  • username
  • group
  • day
  • month
  • year
  • comment

  • I would assume that columns "ID","day","month" and "year" are NUMBER columns. If that is so, then treating them as strings may cause problems (but since I couldn't find any details of your exact problem, I'm only guessing).
    So I suggest you try the following code:
    [Note: Incomplete, not compiled and untested!]

    Hope this helps.
    Good Luck,
    Raymond O'Leary

    Joined: Mar 23, 2004
    Posts: 24
    All working now thanks for the help guys especialyy Avi!
    I agree. Here's the link:
    subject: Auto Increment
    It's not a secret anymore!