JAVA queries the Oracle database and exports it to txt text
Import java. io. fileOutputStream; import java. io. printStream; import java. SQL. statement; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; public class sqltotxt {public static void main (String [] args) throws Exception {Connection conn = null; String SQL; String url = "jdbc: oracle: thin: @ xx. xx. xx. xx: 1521: ORAC ";/* specify the data user name and password */String username =" XXX "; String password =" XXX "; try {Class. forName ("oracle. jdbc. driver. oracleDriver "); conn = DriverManager. getConnection (url, username, password); Statement stmt = conn. createStatement (); SQL = "select * from Table_name"; ResultSet rs = stmt.exe cuteQuery (SQL); // The executeUpdate statement returns an affected number of rows, if-1 is returned, FileOutputStream fos = new FileOutputStream ("E: \ OK .txt", true); // fos. write (rs. getInt (1); PrintStream p = new PrintStream (fos); while (rs. next () {System. out. println (rs. getInt (1) + "," + rs. getInt (2) + "," + rs. getInt (3) + "," + rs. getString (4) + "," + rs. getString (5); // If int type is returned, you can use getInt () String sb = rs. getInt (1) + "," + rs. getInt (2) + "," + rs. getInt (3) + "," + rs. getString (4) + "," + rs. getString (5); // FileOutputStream fos = new FileOutputStream ("E: \ OK .txt", true); // fos. write (rs. getInt (1); // PrintStream p = new PrintStream (fos); p. println (sb);} p. close (); fos. flush ();} catch (SQLException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();} finally {conn. close ();}}}