aspose file tools*
The moose likes JDBC and the fly likes sequence to a field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sequence to a field" Watch "sequence to a field" New topic
Author

sequence to a field

Rinky Deshmukh
Greenhorn

Joined: Jun 09, 2004
Posts: 16
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

Joined: Apr 14, 2004
Posts: 10336

What exactly are you asking?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rinky Deshmukh
Greenhorn

Joined: Jun 09, 2004
Posts: 16
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

Joined: Apr 14, 2004
Posts: 10336

Select it from the sequence first, then use it in your insert statement.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
What db are you using? Most have an "auto increment" / "serial" datatype that will do that for you automatically.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Rinky Deshmukh
Greenhorn

Joined: Jun 09, 2004
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

Database questions like this belong in the "JDBC" forum; I'll move this there for you.


[Jess in Action][AskingGoodQuestions]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Jun 18, 2004
Posts: 20
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 ]

Sudha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sequence to a field
 
Similar Threads
NX: maximum input length of JTextField
help wanted
validating that field has a number
focus problem!
enter only numeric