Java读取定制的sql脚本

本文介绍了一个Java程序,该程序用于从指定路径读取SQL脚本文件,并将其内容逐条解析后发送到Oracle数据库中执行。程序首先建立数据库连接,然后读取本地SQL文件,按分号拆分SQL指令,逐一执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.sky.read;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class ReadSQLFile {
/**
* Oracle数据库连接URL
*/
private final static String DB_URL = "jdbc:oracle:thin:@localhost:1521:ORACL";
/**
* Oracle数据库连接驱
*/
private final static String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
/**
* 数据库用户名
*/
private final static String DB_USERNAME = "gts1031";
/**
* 数据库密码
*/
private final static String DB_PASSWORD = "gts1031";

public static void main(String[] args) {
try {
ReadSQLFile rsf = new ReadSQLFile();
Connection conn = rsf.getConnection();
File f = new File("D:/init.sql");
InputStream is = new FileInputStream(f);
StringBuffer bu = new StringBuffer();
byte[] buf = new byte[1024];
int br = 0;
while ((br = is.read(buf)) != -1) {
bu.append(new String(buf, 0, br));
}
String str = bu.toString();
String[] fileName = str.split("@");
if (fileName != null && fileName.length > 0) {
for (int i = 0; i < fileName.length; i++) {
StringBuffer sb = new StringBuffer();
File file = new File("D:/" + fileName[i].trim());
InputStream in = new FileInputStream(file);
byte[] buff = new byte[1024];
int byteRead = 0;
while ((byteRead = in.read(buff)) != -1) {
sb.append(new String(buff, 0, byteRead));
Statement stmt = conn.createStatement();
String sqlStr = sb.toString();
String[] sql = sqlStr.split(";");
if (sql != null && sql.length > 0) {
for (int j = 0; j < sql.length; j++) {
try {
stmt.executeUpdate(sql[j]);
} catch (Exception e) {
// 打印日志
e.printStackTrace();
continue;
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取数据库连接
*
* @return Connection
*/
public Connection getConnection() {
/**
* 声明Connection连接对象
*/
Connection conn = null;
try {
/**
* 加载驱动
*/
Class.forName(DB_DRIVER);
/**
* 获取数据库连接
*/
conn = DriverManager
.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public void closeConnection(Connection conn) {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值