aspose file tools*
The moose likes JDBC and the fly likes Auto Increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Auto Increment" Watch "Auto Increment" New topic
Author

Auto Increment

Raymond O'Leary
Greenhorn

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 -
Stmt.setString(1,"");
-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");
Stmt.setString(1,"");
Stmt.setString(2,request.getParameter("UName"));
........
Stmt.setString(6,request.getParameter("xxx"));
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
Greenhorn

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.*"%>
<html>
<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 racle:thin:@witnt07.wit.ie:1521 rawit","X", "X");
String query = "INSERT INTO SUPERLOG VALUES ('',?,?,?,?,?,?)";
PreparedStatement Stmt = con.prepareStatement(query);

String UName = (String) session.getAttribute("username");
Stmt.setString(1,NEXTSUPERLOG.nextval);
Stmt.setString(2,request.getParameter("UName"));
Stmt.setString(3,request.getParameter("t1"));
Stmt.setString(4,request.getParameter("D2"));
Stmt.setString(5,request.getParameter("D3"));
Stmt.setString(6,request.getParameter("D4"));
Stmt.setString(7,request.getParameter("comment"));
Stmt.executeUpdate();
Stmt.close();
con.close();
%>
<p> <br><p> <br><p> <H3><b>Your Comment Was Successfully Added</b></h3>
</body>
</html>
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
Greenhorn

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);
rs1.close();
con.close();
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: 1135

Raymond,
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,
    Avi.
    Raymond O'Leary
    Greenhorn

    Joined: Mar 23, 2004
    Posts: 24
    All working now thanks for the help guys especialyy Avi!
    Ray
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Auto Increment