gpt4 book ai didi

java - JDBC + ScriptRunner : error with runScript

转载 作者:行者123 更新时间:2023-12-01 05:48:22 27 4
gpt4 key购买 nike

我想使用ScriptRunner使用 JDBC 驱动程序执行 sql 脚本文件。我可以启动 ScriptRunner,但无法执行 runScript 行:

ScriptRunner runner = new ScriptRunner(c, false, false);
runner.runScript("C:/Users/Pierre/Documents/create.sql");

错误是:

cannot find symbol method runScript(java.lang.String) || line 41

与数据库的连接良好。

import java.sql.*;

public class ConnectPostgreSQL {
public static void main(String[] argv) {
System.out.println("Checking if Driver is registered with DriverManager.");

try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}

System.out.println("Registered the driver ok, so let's make a connection.");

Connection c = null;

try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "passroot");
} catch (SQLException se) {
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}

if (c != null)
System.out.println("Hooray! We connected to the PostgreSQL database!");
else
System.out.println("We should never get here.");

//temps t1
long begin = System.currentTimeMillis();
System.out.println(begin);

ScriptRunner runner = new ScriptRunner(c, false, false);
runner.runScript("C:/Users/Pierre/Documents/create.sql");

//temps t2
long end = System.currentTimeMillis();
System.out.println(end);

//différence de t2 - t1
float time = ((float) (end-begin)) / 1000f;
System.out.println(time);
}
}

有人可以帮助我吗?谢谢!

最佳答案

这是因为runScript方法没有参数字符串,查看ScriptRunner code

public void runScript(Reader reader)

改变你的

runner.runScript("C:/Users/Pierre/Documents/create.sql");

致:

try {
FileReader reader = new FileReader("C:/Users/Pierre/Documents/create.sql");
runner.runScript(reader);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

并添加导入,例如

import java.io.BufferedReader;
import java.io.FileReader;

关于java - JDBC + ScriptRunner : error with runScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5319486/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com