如何使用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\".
看来我做错了...     

解决方法

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