Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting data at runtime into Mysql using JDBC

 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
I want to take inputs from console and save the data entered into the MySQL using jdbc. Following is the code I tried to make but this code is notable to enter the data into the table . It just increment the primary key.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at line 72. You're exeuting the following SQL statement:

INSERT INTO OHRI VALUES (id ,title , author)

Note that you are not filling in the values of the variables id, title and author; instead, you're executing a statement which literally contains the names of the variables "id", "title" and "author". That's not going to work - the database is not going to understand what these words mean.

You should use a PreparedStatement instead and set the values before you execute the statement.

 
Tarun Oohri
Ranch Hand
Posts: 189
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Look at line 72. You're exeuting the following SQL statement:

INSERT INTO OHRI VALUES (id ,title , author)

Note that you are not filling in the values of the variables id, title and author; instead, you're executing a statement which literally contains the names of the variables "id", "title" and "author". That's not going to work - the database is not going to understand what these words mean.

You should use a PreparedStatement instead and set the values before you execute the statement.



Thanks dear... got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic