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);
}
}
{
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);
}
}