Reputation: 153
I use Eclipse. My goal is to connect to the PostgreSQL database that I've already created on my machine and create some tables in it. As far as I understand I can do this by creating an sql file in Eclipse. Unfortunately, I don't figure out what to do after creating an SQL file so that I could connect to my database.
P.S. I managed to do this using 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();
}
}
}
But my question is "Is it possible to do the same through creating an SQL file"?
Upvotes: 0
Views: 4453
Reputation: 60
First you should have the Postgres JDBC driver for the using Java. You can download it from https://jdbc.postgresql.org/download.html.
So now it's the Eclipse config:
Upvotes: 2