如何使用JDBC执行一组相关的SQL指令?

问题描述

| 我正在尝试通过JDBC(它是SQL Server)执行此脚本:
DECLARE @var VARCHAR(10)
SET @var = \"test\"
INSERT INTO foo (name) VALUES (@var)
这只是一个例子,在我的业务案例中,我在一个脚本中收集了大量SQL语句。 我正在尝试:
final Statement stmt = connection.createStatement();
for (String sql : lines) {
  stmt.executeUpdate(sql);
}
stmt.close();
SQL Server在第二行说:
java.sql.SQLException: Must declare the scalar variable \"@var\".
看来我做错了...     

解决方法

        您一次要执行一行,所以第二行当然会失败,因为它取决于第一行。为什么不立即执行全部操作呢?     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...