【问题标题】:How to connect to the local database in eclipse?如何在eclipse中连接到本地数据库?
【发布时间】: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


【解决方案1】:

首先,您应该拥有用于使用 Java 的 Postgres JDBC 驱动程序。您可以从https://jdbc.postgresql.org/download.html下载。

所以现在是 Eclipse 配置:

  1. 打开 DB 开发透视图窗口 > 打开透视图 > 其他 > 数据库开发透视图
  2. 选择 PostgresSQL 配置文件
  3. 选择您已下载的驱动程序
  4. 在向导中输入数据库详细信息,然后按“测试连接”按钮以验证一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-13
    • 2013-02-05
    • 2017-09-01
    • 1970-01-01
    • 2015-07-09
    • 2020-08-07
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多