import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Oracle2Txt {
public static void main(String[] args) {
File f = new File("D:\\test.txt");
Writer out = null ;
Connection conn = null;
PreparedStatement pstms = null;
ResultSet rs = null;
try{
out = new FileWriter(f,false);
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","username","password");
String sql = "select * from stu";
pstms = conn.prepareStatement(sql);
rs = pstms.executeQuery();
while(rs.next()){
String str = rs.getString("num_code");
String str2 = rs.getString("name");
out.write(str+" ");
out.write(str2+" " + "\n");//换行
out.flush();
System.out.println(rs.getString("num_code")+" "+rs.getString("name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
if(out != null){
out.close();
}
if(rs != null){
rs.close();
}
if(pstms != null){
pstms.close();
}
if(conn != null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Oracle2Txt {
public static void main(String[] args) {
File f = new File("D:\\test.txt");
Writer out = null ;
Connection conn = null;
PreparedStatement pstms = null;
ResultSet rs = null;
try{
out = new FileWriter(f,false);
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","username","password");
String sql = "select * from stu";
pstms = conn.prepareStatement(sql);
rs = pstms.executeQuery();
while(rs.next()){
String str = rs.getString("num_code");
String str2 = rs.getString("name");
out.write(str+" ");
out.write(str2+" " + "\n");//换行
out.flush();
System.out.println(rs.getString("num_code")+" "+rs.getString("name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
if(out != null){
out.close();
}
if(rs != null){
rs.close();
}
if(pstms != null){
pstms.close();
}
if(conn != null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}