*
The moose likes JDBC and the fly likes Inserting data at runtime into Mysql using JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting data at runtime into Mysql using JDBC" Watch "Inserting data at runtime into Mysql using JDBC" New topic
Author

Inserting data at runtime into Mysql using JDBC

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
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
 
 
subject: Inserting data at runtime into Mysql using JDBC
 
Similar Threads
JDBC: What is the concrete class that implements the jdbc interfaces
Not to execute console based jdbc application
java.sql.SQLException: ORA-01006: bind variable does not exist
How to create SQL server2005 database on remote machine using jdbc?
Servlet is not working