简体   繁体   English

如何运行.sql脚本JDBC?

[英]How run .sql script JDBC?

i have.sql file with script:我有 .sql 文件和脚本:

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:我正在尝试运行此文件以使用以下代码在我的 PostgreSQL 数据库中创建表:

    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)在您的 sql 脚本中添加以下内容:---(您应该先创建数据库)

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.希望它能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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