Marcos Aguayo
Marcos Aguayo

Reputation: 7200

Using psql in Java

I'm using PostgreSQL in Java and I want to create a database structure.

How I can run a SQL script using psql?

Upvotes: 0

Views: 183

Answers (2)

Marcos Aguayo
Marcos Aguayo

Reputation: 7200

I found this code that works like psql:

public void ejecutarScript(String file) throws IOException, SQLException {
    Statement st3 = conexion.createStatement();
    BufferedReader in = new BufferedReader(new FileReader(file));
    String str;
    StringBuffer sb = new StringBuffer();
    while ((str = in.readLine()) != null) {
        sb.append(str + "\n ");
    }
    in.close();
    st3.executeUpdate(sb.toString());
}

Upvotes: 1

David
David

Reputation: 20063

The same way you would any other SQL script, just make sure you've set the dialogue to PSQL. If you're using JDBC make sure you're using the driver found here

Upvotes: 1

Related Questions