java 通过调用JDBC连接Oracle;执行SQL*PlUS 导入sql脚本

来源:转载

package com.zht;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC_IMP {

private String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private String theUser = "test";
private String thePw = "sa";
private Connection c = null;
private Statement conn;
private ResultSet rs = null;

public void conJDBC() {
try {
System.out.println("开始连接");
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
c = DriverManager.getConnection(dbUrl, theUser, thePw);
conn = c.createStatement();
System.out.println("连接正常----------");
} catch (Exception e) {
System.out.println("连接异常");
}
}

public boolean executeUpdate(String sql) {
try {
conn.executeUpdate(sql);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}

public ResultSet executeQuery(String sql) {
rs = null;
try {
rs = conn.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

public void close() {
try {
conn.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void imp(String fileName) {
try {
System.out.println("导入开始-----");
// 登网站
Process process = Runtime
.getRuntime()
.exec(
"sqlplus test/[email protected] @g:/ccc.sql commit on");
System.out.println("导入结束-----");
} catch (Exception e) {
System.out.println("导入异常-----");
}
}

public static void main(String[] args) {
ResultSet rs;
JDBC_IMP conn = new JDBC_IMP();
conn.conJDBC();
rs = conn.executeQuery("select * from aaa");
try {
while (rs.next()) {
System.out.println(rs.getInt(1) + "=====" + rs.getInt(2));
}
// 查询到的信息写入到本地sql文件
// 开始进行数据库导入操作
conn.imp("");
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
System.out.println("连接关闭");
}

}

}



分享给朋友:
您可能感兴趣的文章:
随机阅读: