Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sequence to a field

 
Rinky Deshmukh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a table where data is entered using a form.there is an id field which has to take values automatically i.e increament the field value as other fields are entered.
pls can any one help me.

thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you asking?
 
Rinky Deshmukh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a table which has 5 fields one of them is integer(id) .i wanted to assign values to it automatically in sequence using java as i'm entering other values using sql query in java



PreparedStatement st=con.prepareStatement("insert into spotlight(ID IN SEQUENCE,req_name,req_desc,status,start_date) values(?,?,?,?) ");
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Select it from the sequence first, then use it in your insert statement.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What db are you using? Most have an "auto increment" / "serial" datatype that will do that for you automatically.
 
Rinky Deshmukh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm sorry i'm not able to understand
i tried as:

int i=0;
statement s=con.createStatement();
resultSet rs=s.executeQuery("select max(req_id) from spotlight");
i=rs.getInt(1);
if(i==0)
{
i=1;
}
else
{
i=i+1;
}
st=con.prepareStatement("insert into table_name(req_id,req_name) values(?,?) ");

st.setInt(1,i);
st.setString(2,name);
but i'm getting INVALID CURSOR STATE exception
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Database questions like this belong in the "JDBC" forum; I'll move this there for you.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've re-read your posts: you are not actually using a sequence are you? Be careful before you rush to answer that - "sequence" (in this context) is an Oracle/Postgres specific term, so if its not either you are using, then ignore my previous post. If however it is, the syntax for selecting the next value from a sequence is "select seq_name.nextval from dual"/"select nextval('sequence_name')" respecitively - not using the max() function. If you are using another RDBMS then you probably have an autonumber or serial datatype as Blake Minghelli points out. In which case, just leave the PK out of your insert statement and let the DB handle it. i.e.:

[ June 17, 2004: Message edited by: Paul Sturrock ]
 
jaya sudha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Everybody,


First create a sequence in the DB then use the sequence in the insert statment like this

st=con.prepareStatement("insert into table_name(req_id,req_name) values(?,?) ");

st.setInt(1,sequencename.nextval);
st.setString(2,name);
Pls try this
[ June 18, 2004: Message edited by: jaya sudha ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic