Forums Register Login

Auto Increment

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
You can specify the fields to set, excluding the auto-increment one.
sql = "INSERT INTO tablename(someField, someOtherField, ...) VALUES(?,?,...)"
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Can't see the forest for the trees as the saying goes.
+Pie Number of slices to send: Send
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.
    +Pie Number of slices to send: Send
    All working now thanks for the help guys especialyy Avi!
    Ray
    I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 8814 times.
    Similar Threads
    Auto Increment ID Field Table in the Oracle Database
    how to keep register page data if register fails
    JDBC Driver, Oracle sequence number problems
    Pass radio button/ text box responses from one page to another
    handling wrong data type entry through servlet
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 19, 2024 01:03:27.