• 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

sequence to a field

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly are you asking?
 
Rinky Deshmukh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Select it from the sequence first, then use it in your insert statement.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic