oracle sqlldr用法

public class ConnForOracle
{
private String url = "jdbc:oracle:thin:@localhost:1521:ORCL";

private String userName = "oscar";

private String pwd = "oscar";

public Connection getConnection()
{
Connection con = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url, userName, pwd);
if (con != null)
{
System.out.println("success");
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}

/**
* 得到oracle连接,防止连接挂死
* @param timout 超时时间
* @param driverName 驱动name
* @param url 数据库连接符
* @param username 用户名
* @param password 密码
* @return 数据库连接
*/
public static Connection getOracleConn(Long timout, String driverName, final String url,
final String username, final String password) throws Exception
{
Connection connection = null;
java.util.concurrent.Future<Connection> future = null;
ExecutorService executor = Executors.newSingleThreadExecutor();
try
{
Class.forName(driverName);

Callable<Connection> task = new Callable<Connection>()
{
public Connection call() throws Exception
{
return DriverManager.getConnection(url, username, password);
}
};
future = executor.submit(task);
connection = future.get(timout, TimeUnit.SECONDS);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (future != null)
{
future.cancel(true);
}
executor.shutdown();
}
return connection;
}

public void readAndWrite()
{
ConnForOracle con = new ConnForOracle();
Connection conn = con.getConnection();
String sql = "select count(1) from broadbandmonitor";
String sql1 = "select t.id,t.internetusername from broadbandmonitor t ";
Statement stm = null;
ResultSet rs = null;
BufferedWriter buw = null;
try
{
buw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://aaa.txt")));
stm = conn.createStatement();
rs = stm.executeQuery(sql1);
String id = null;
String name = null;
String str = "";
// if (rs.next())
// {
// int count = (int) rs.getLong(1);
// System.out.println(count + "$$");
// }
while (rs.next())
{
id = rs.getString(1);
name = rs.getString(2);
str = id + "," + name;
buw.write(str);
buw.newLine();
}
System.out.println("done");
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (stm != null)
{
try
{
stm.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (rs != null)
{
try
{
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (buw != null)
{
try
{
buw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

public void sqlload(Connection conn)
{
Process process;
try
{
// 调用cmd命令
process = Runtime
.getRuntime()
.exec(
"cmd.exe /c sqlldr userid=oscar/oscar@ORCL.CHINA control=d:\\aaa.ctl log=d:\\logx.log direct=true");
process.waitFor();
System.out.println("load succ");
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}

/** sql load for mysql */
// StringBuffer sb = new StringBuffer();
// sb.append("load data local infile \"");
// sb.append("D://aaa.txt");
// sb.append("\" into table test");
// sb.append(" FIELDS TERMINATED BY ',' ");
// try
// {
// System.out.println(sb.toString());
// conn.createStatement().execute(sb.toString());
// System.out.println("load succ");
// }
// catch (SQLException e)
// {
// e.printStackTrace();
// }
finally
{
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}

public static void main(String[] args)
{
ConnForOracle con = new ConnForOracle();
Connection conn = con.getConnection();
// con.readAndWrite();
con.sqlload(conn);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值