jdbc mysql select,使用JDBC在MySQL中插入select

博客内容讲述了在尝试从一个表中选取数据并插入到另一个表时遇到的SQL语法错误。作者指出了错误的原因——使用了错误的DATEADD函数语法,并提供了修正后的SQL语句,删除了不必要的VALUES关键字,并调整了DATE_ADD函数的用法。
摘要由CSDN通过智能技术生成

I would like to have a value from a row inserted into an other row here is my code:

static void addVipMonth(String name) throws SQLException

{

Connection conn = (Connection) DriverManager.getConnection(url, user, pass);

PreparedStatement queryStatement = (PreparedStatement) conn.prepareStatement("INSERT INTO vips(memberId, gotten, expires) " +

"VALUES (SELECT name FROM members WHERE id = ?, NOW(), DATEADD(month, 1, NOW()))"); //Put your query in the quotes

queryStatement.setString(1, name);

queryStatement.executeUpdate(); //Executes the query

queryStatement.close(); //Closes the query

conn.close(); //Closes the connection

}

This code is not valid. How do I correct it?

解决方案

I get an error 17:28:46 [SEVERE] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MyS QL server version for the right syntax to use near ' NOW(), DATE_ADD( now(), INT ERVAL 1 MONTH )' at line 1 – sanchixx

It was due to error in SELECT .. statement.

Modified statement is:

INSERT INTO vips( memberId, gotten, expires )

SELECT name, NOW(), DATE_ADD( now(), INTERVAL 1 MONTH )

FROM members WHERE id = ?

You don't require VALUES key word when inserting with a select.

You used a wrong DATEADD function syntax. Correct syntax is Date_add( date_expr_or_col, INTERVAL number unit_on_interval).

You can try your insert statement as corrected below:

INSERT INTO vips( memberId, gotten, expires )

SELECT name FROM members

WHERE id = ?, NOW(), DATE_ADD( now(), INTERVAL 1 MONTH )

Refer to:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值