This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Auto Increment

 
Raymond O'Leary
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can specify the fields to set, excluding the auto-increment one.
sql = "INSERT INTO tablename(someField, someOtherField, ...) VALUES(?,?,...)"
 
Raymond O'Leary
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't see the forest for the trees as the saying goes.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 24
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    All working now thanks for the help guys especialyy Avi!
    Ray
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic