Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert data into mySQL table with java

Tags:

java

sql

mysql

jdbc

I have a predefined table in a mySQL database: enter image description here

I am working on saving data inputted from the user to the database but I cant seem to any data to save in the database. With the following code I am trying to update the first row of the database (ID: 1 through OTHER 2: 0). What am I doing wrong?

private java.sql.Connection con = null;
private PreparedStatement pst = null;
private ResultSet rs = null;
private String url = "jdbc:mysql://localhost:8889/deliveryEarn";
private String user = "root";
private String password = "root";

try {
     con = DriverManager.getConnection(url, user, password);
     Statement st = (Statement) con.createStatement(); 

     st.executeUpdate("INSERT INTO incomeCalc " + "VALUES (3, 75, 6, 25, 18.50)");

     con.close();
}

catch (SQLException ex) {
     Logger lgr = Logger.getLogger(deliveryMain.class.getName());
     lgr.log(Level.SEVERE, ex.getMessage(), ex);

 } 
like image 617
David Tunnell Avatar asked Aug 10 '12 14:08

David Tunnell


People also ask

How do you add values in Java?

You should quote string in the insert statement. String query = "insert into customer values (null, '"+fname+"', '"+lname+"', '"+tel+"', '"+email+"', '"+password+"')"; A more appropriate solution is to insert placeholder in the query string, prepare the query, and pass in the execute method the values of the variables.

Can Java work with MySQL?

In Java, we can connect to our database(MySQL) with JDBC(Java Database Connectivity) through the Java code. JDBC is one of the standard APIs for database connectivity, using it we can easily run our query, statement, and also fetch data from the database.

How do we insert store a file into MySQL database using JDBC?

Storing text file using JDBC: CREATE TABLE Articles(Name VARCHAR(255), Article LONGTEXT); Now, using JDBC connect to the database and prepare a PreparedStatement to insert values into the above created table: String query = "INSERT INTO Tutorial(Name, Article) VALUES (?,?)";PreparedStatement pstmt = con.

What is insert query in Java?

SQL INSERT statement is a SQL query. It is used to insert a single or a multiple records in a table. There are two ways to insert data in a table: By SQL insert into statement. By specifying column names.


3 Answers

I think it will not work because the number of values is less than the number of columns in your table. What you have to do is to specify the name of columns to match the number of your values.

INSERT INTO incomeCalc VALUES (3, 75, 6, 25, 18.50)  // error
// the only way this will work is when you have only 5 columns in 
// your table but in your case you have 7 that is why it will not work

it should be

INSERT INTO incomeCalc(specify columns here to the values bound to)
VALUES (3, 75, 6, 25, 18.50)

w3School: (INSERT)

It is possible to write the INSERT INTO statement in two forms.

The first form doesn't specify the column names where the data will be inserted, only their values:

INSERT INTO table_name
VALUES (value1, value2, value3,...)

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
like image 140
John Woo Avatar answered Sep 21 '22 12:09

John Woo


Your insert statement should be:

"INSERT INTO incomeCalc(ID, TIPS, HOURS, GAS, HOURLY_EARNINGS) " +
"VALUES (3, 75, 6, 25, 18.50)"

In other words your statement is missing column names.

like image 21
munyengm Avatar answered Sep 22 '22 12:09

munyengm


You need to specify the column names for example:

"INSERT INTO incomeCalc (ID, TIPS, HOURS, GAS, HOURLY_EARNINGS) VALUES (3, 75, 6, 25, 18.50)"
like image 26
Lo Juego Avatar answered Sep 20 '22 12:09

Lo Juego