java mysql连接两张表,如何使用Java和MySQL在一个语句中插入两个不同的表?

I am using Java, Spring (NamedParameterJdbcTemplate) and MySQL. My statement looks like this:

INSERT INTO Table1 (Name) VALUES (?);

INSERT INTO Table2 (Path, Table1Id) VALUES (?, LAST_INSERT_ID())

But it is throwing the following error:

PreparedStatementCallback; bad SQL grammar [INSERT INTO Table1 (Name) VALUES (?);

INSERT INTO Table2 (Path, Table1Id) VALUES (?, LAST_INSERT_ID())] `

Nested exception is:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO Table2 (Path, Table1Id' at line 1

The syntax works fine in MySQL but something is up when combining via the Spring template.

Thanks!

解决方案

Use the addBatch method to run multiple statements

Statement stmt = con.createStatement();

stmt.addBatch(

"update registration set balance=balance-5.00

where theuser="+theuser);

stmt.addBatch(

"insert into auctionitems(

description, startprice)

values("+description+","+startprice+")");

int[] results = stmt.executeBatch();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设我们有两个:`users`和`orders`,`users`存储用户信息,`orders`存储订单信息,两个之间通过`user_id`字段进行关联。 以下是一个简单的增删改查Java代码示例: ```java import java.sql.*; public class Main { // 定义数据库连接信息 static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/mydatabase"; static final String USER = "root"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开连接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 stmt = conn.createStatement(); String sql = "SELECT * FROM users"; ResultSet rs = stmt.executeQuery(sql); // 处理结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email); } // 插入数据 sql = "INSERT INTO orders (user_id, total_price) VALUES (1, 100)"; stmt.executeUpdate(sql); // 更新数据 sql = "UPDATE users SET email='new_email@example.com' WHERE id=1"; stmt.executeUpdate(sql); // 删除数据 sql = "DELETE FROM orders WHERE id=1"; stmt.executeUpdate(sql); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (stmt != null) stmt.close(); } catch (SQLException se2) {} try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } } } ``` 在这个示例,我们使用`java.sql`包的`Connection`和`Statement`类来执行SQL查询、插入、更新和删除操作。我们首先定义了数据库连接信息和驱动程序,然后打开连接并执行一个SELECT查询。在处理结果集后,我们插入一个新订单,更新了用户的电子邮件地址,并删除了一个订单。 需要注意的是,在实际应用程序,我们应该使用预处理语句和绑定变量来避免SQL注入攻击。此外,我们也应该使用ORM框架来简化数据库访问过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值