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("導(dǎo)入開始-----");
// 登網(wǎng)站
Process process = Runtime
.getRuntime()
.exec(
"sqlplus test/sa@orcl @g:/ccc.sql commit on");
System.out.println("導(dǎo)入結(jié)束-----");
} catch (Exception e) {
System.out.println("導(dǎo)入異常-----");
}
}
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文件
// 開始進(jìn)行數(shù)據(jù)庫導(dǎo)入操作
conn.imp("");
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
System.out.println("連接關(guān)閉");
}
}
}
聯(lián)系客服