• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Auto Increment

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't see the forest for the trees as the saying goes.
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All working now thanks for the help guys especialyy Avi!
    Ray
     
    reply
      Bookmark Topic Watch Topic
    • New Topic