gpt4 book ai didi

java - 如何通过 Java 代码创建和删除数据库、模式 PostgreSQL?

转载 作者:行者123 更新时间:2023-11-29 14:10:19 26 4
gpt4 key购买 nike

我试试

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import com.ibatis.common.jdbc.ScriptRunner;

public static void createDatabase() throws ClassNotFoundException, SQLException {
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/postgres", "postgres", "123456a@");
Statement stmt = connection.createStatement();
stmt.executeQuery("CREATE DATABASE IF NOT EXISTS foo");
stmt.executeQuery("USE foo");
connection.close();
}

public static void dropDatabase() throws ClassNotFoundException, SQLException {
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/", "postgres", "123456a@");
Statement statement = connection.createStatement();
statement.executeUpdate("DROP DATABASE foo");
connection.close();
}

但是create,also drop方法不成功。

调用创建方法时出错:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "NOT"
Position: 20
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2453)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2153)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:286)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:432)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:358)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:305)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:291)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:269)
at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:236)
at com.nttdata.RunSqlScript.createDatabase(RunSqlScript.java:57)
at com.nttdata.RunSqlScript.main(RunSqlScript.java:27)

调用drop方法时出错:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: database "foo" is being accessed by other users
Detail: There is 1 other session using the database.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2453)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2153)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:286)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:432)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:358)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:305)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:291)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:269)
at org.postgresql.jdbc.PgStatement.executeUpdate(PgStatement.java:249)
at com.nttdata.RunSqlScript.dropDatabase(RunSqlScript.java:71)
at com.nttdata.RunSqlScript.main(RunSqlScript.java:28)

最佳答案

首先,您的问题中创建数据库时使用的 SQL 语法不正确。堆栈跟踪说明了语法不正确的全部。

如果你想检查数据库是否存在,那么你可能必须在你的 Java 代码中做这样的事情:

ResultSet rs = stmt.executeQuery("select datname from pg_database where datname like 'foo';");

不是通过 IF NOT EXISTS 方法

访问此rs 对象会让您知道数据库是否存在。然后,您可以相应地启动 CREATEDELETE 数据库操作。

String databaseName = "";
if(rs.next()) {
databaseName = rs.getString("datname");
}
stmt.executeQuery("DROP DATABASE " + databaseName);

如果直接 DROP DATABASE 不起作用(我遇到过很多次),您可以考虑使用 dropdb 实用程序或通过以下方法之一方法。

方法一

使用以下查询来防止将来连接到您的数据库:

REVOKE CONNECT ON DATABASE foo FROM public;

然后您可以终止与此数据库的所有连接,但您自己的除外:

SELECT pid, pg_terminate_backend(pid) 
FROM pg_stat_activity
WHERE datname = current_database() AND pid <> pg_backend_pid();

由于您已经撤销了对相应数据库的 CONNECT 权限,因此外部自动连接 将不再能够这样做。您现在可以毫无问题地删除数据库。

方法 2:

此方法采用批处理作业方式,您可以从相应的 jar 中调用此类:

Process batchProcess = Runtime.getRuntime().exec("C:/Program Files/PostgreSQL/9.5/bin/psql -h \"DB SERVER ADDRESS\" -U postgres -f C:/batch.sql");

batch.sql 包含 SQL DROP DATABASE 语句,执行时将被删除。

希望这对您有所帮助!

关于java - 如何通过 Java 代码创建和删除数据库、模式 PostgreSQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39523429/

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