我正在尝试使用“\i E:/dump.sql”命令从 postgres 中的 .sql 文件导入数据库,它在 postgres 命令提示符下工作正常,但是当我从 java 中尝试相同的操作时,它会在“\“,我的代码是
connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/database","postgres", "passwd");
PreparedStatement ps3 = connection.prepareStatement("\\i E:/dump.sql");
boolean res3= ps3.execute();
System.out.println("imported succesfully .."+res3);
最佳答案
使用 JDBC 驱动程序/接口(interface),您只能使用 SQL,您尝试的是发出 PostgreSQL 命令行工具 (psql
) 特定命令。那是行不通的。
如果您坚持这样做,您可以使用 Runtime.getRuntime().exec(...)
方法,例如
Runtime.getRuntime().exec( "psql -f dump.sql" );
干杯,
关于java - 使用 java 中的\i 命令在 postgres 中导入 .sql 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17469424/