简体   繁体   English

使用java将数据插入mySQL表

[英]Insert data into mySQL table with java

I have a predefined table in a mySQL database: 我在mySQL数据库中有一个预定义的表: 在此输入图像描述

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). 使用以下代码,我试图更新数据库的第一行(ID:1到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) w3School :(插入)

It is possible to write the INSERT INTO statement in two forms. 可以用两种形式编写INSERT INTO语句。

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 . A。 Either pass the values for all the columns of the table in the order which they are defined in MYSQL DB. 按照在MYSQL DB中定义的顺序传递表的所有列的值。 Here we can see 7 columns in your table and you are providing only 5 values... 在这里,我们可以在您的表中看到7列,您只提供5个值...

or 要么

B . 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)" 像munyengm你在rcase中的陈述是:“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. 但你可能会循环值和东西,所以我建议使用预备语句,这样可以防止sql注入。

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; First READONLY = false;

There are some restrictions.You need to put [Name_table$] and \\ before and after the COLUMN_NAMES . 有一些限制。你需要在COLUMN_NAMES之前和之后放置[Name_table$]\\ 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)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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