Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

JDBC GUI autoincrement field

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! So I am working with JDBC, postgres and eclipse. I am inserting a string in the second column of a postgres database. The program is obviously in java. I have googled and I feel I'm on the right track but something is not right because it's not working. there are 2 fields, an autoincrement "id" field and a question field. I am only updating the question field, but I need a way to get the id field's current value so I can increment it or find a way to let it increment itself when I insert a string into the question field. Here is what I have so far:



This is a GUI app and I also want to add questions as many times as is necessary.

Thank you!
 
Marshal
Posts: 25472
67
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adam Grey wrote:I need a way to get the id field's current value so I can increment it or find a way to let it increment itself when I insert a string into the question field.



Yes, that's right. And the way to do it is to not update it at all. Let the database deal with that, as it's designed to do. As you said:

I am only updating the question field



Exactly! Write an SQL statement which only updates the question field.

(And I moved this thread to the JDBC forum, since it has nothing to do with Swing.)
 
Adam Grey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(sorry did not even see the JDBC forum)

Ok, well when I try that, like so:



It gives me a null value error. (violates not null constraint) Even after I put something in there for the value 0 and 1 (manually entered to database)
 
Paul Clapham
Marshal
Posts: 25472
67
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the face of it, your SQL writes a null value to the "id" field. But when the database sees that the "id" field is an auto-increment column, it will take that as meaning it should fill the column with the auto-increment value.

But if you didn't declare the column to be auto-increment but you did declare it as not null, you're going to get that message.

(And yes, that's what your SQL statement should look like.)
 
Adam Grey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's both auto-increment and not null. What should I do?
 
Adam Grey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe there is something wrong with my schema.
 
Paul Clapham
Marshal
Posts: 25472
67
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adam Grey wrote:It gives me a null value error. (violates not null constraint)



You didn't actually say that was referring to the "id" column, I just assumed that. Perhaps the "question" column is not-null and you tried to insert a null value. After all I don't see any code which sets that variable to a non-null value.

Or perhaps the error message tells you what column has the problem...
 
Adam Grey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, well thank you! You made me realize the mistake. I guess somebody did not set it to autoincrement! Thats 4 hours of my life I won't be able to get back. -
 
Paul Clapham
Marshal
Posts: 25472
67
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Onwards to the next time-sucking problem (it's all part of the fun of being a programmer)...
 
    Bookmark Topic Watch Topic
  • New Topic