【发布时间】:2021-02-12 16:48:36
【问题描述】:
我使用 Eclipse。我的目标是连接到我已经在我的机器上创建的 PostgreSQL 数据库并在其中创建一些表。据我了解,我可以通过在 Eclipse 中创建一个 sql 文件来做到这一点。不幸的是,我不知道创建 SQL 文件后要做什么才能连接到我的数据库。
附:我设法使用 JDBC 做到了这一点:
package com.foxminded.table_builder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.foxminded.table_model.CoursesTable;
import com.foxminded.table_model.GroupsTable;
import com.foxminded.table_model.StudentsTable;
public class TableBuilder {
private static final String USER = "user1";
private static final String PASSWORD = "01234";
private static final String URL = "jdbc:postgresql://localhost:5432/school";
StudentsTable studentsTable = new StudentsTable();
GroupsTable groupsTable = new GroupsTable();
CoursesTable coursesTable = new CoursesTable();
public void buildTable() throws SQLException {
Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
try (Statement statement = connection.createStatement()){
statement.execute(studentsTable.buildTable());
statement.execute(groupsTable.buildTable());
statement.execute(coursesTable.buildTable());
}finally {
connection.close();
}
}
}
但我的问题是“是否可以通过创建 SQL 文件来做同样的事情”?
【问题讨论】:
-
“是否可以通过创建 SQL 文件来做同样的事情” 是什么意思?目前尚不清楚您要达到的目标。
标签: java postgresql jdbc