简体   繁体   中英

Insert data into mySQL table with java

I have a predefined table in a mySQL database: 在此输入图像描述

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);

 } 

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,...)

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.

您需要指定列名称,例如:

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

A . Either pass the values for all the columns of the table in the order which they are defined in MYSQL DB. Here we can see 7 columns in your table and you are providing only 5 values...

or

B . Make use of this syntax

INSERT INTO TABLE_NAME (COLUMN_NAMES_SEPARATED_BY_COMMA) VALUES (VALUES_SEPARATED_BY_COMMA)

like munyengm the statement in you rcase would be : "INSERT INTO incomeCalc(ID, TIPS, HOURS, GAS, HOURLY_EARNINGS) " + "VALUES (3, 75, 6, 25, 18.50)"

but you probably going to loop value and stuff so I would recommend using a prepared statement instead, this prevent sql injection.

ps = "INSERT INTO incomeCalc(ID, TIPS, HOURS, GAS, HOURLY_EARNINGS) VALUES (?, ?, ?, ?, ?)"

then

        ps.setInt(index++, id); //id=3
        ps.setInt(index++, tips); //tips=75
        ps.setInt(index++, hours);//hour=6
        ps.setInt(index++, gas);//gas=25
        ps.setFloat(index++, HOURLY_EARNINGS);

First READONLY = false;

There are some restrictions.You need to put [Name_table$] and \\ before and after the COLUMN_NAMES . like this:

st.executeUpdate("INSERT INTO [IncomeCalc$] (\"ID\", \"TIPS\", \"HOURS\", \"GAS\",\"HOURLY_EARNINGS\") VALUES ('2012-10-25 01:00:00','16.3')");

这种格式对我有用,就像你看到的那样,没有额外的逗号,斜线或其他任何东西是必要的:

statement.executeUpdate("INSERT INTO incomeCalc (value1, value2, value99) VALUES (3, 75, 6)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM