简体   繁体   中英

How run .sql script JDBC?

i have.sql file with script:

DROP TABLE IF EXISTS groups;
CREATE TABLE groups(
group_id INTEGER NOT NULL,
group_name VARCHAR(50),
PRIMARY KEY(group_id)
);

And im trying to run this file to create table in my PostgreSQL DB using this code:

    String URL = "jdbc:postgresql://localhost:5432/school";
    String user = "postgres";
    String password = "password";

    Connection connection = DriverManager.getConnection(URL, user, password);
    System.out.println("Success.........");
    ScriptRunner scriptRunner = new ScriptRunner(connection);
    Reader reader = new BufferedReader(new FileReader("src/main/resources/database/dbScript.sql"));
    scriptRunner.runScript(reader);

but table not generating. what am I doing wrong?

Add below in your sql script:--- ( you are supposed to create database first)

SET AUTOCOMMIT = ON
CREATE DATABASE school;
use school;
DROP TABLE IF EXISTS groups;
CREATE TABLE school.groups(
group_id INTEGER NOT NULL,
group_name VARCHAR(50),
PRIMARY KEY(group_id)
);

hope it will resolve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM