JAVA queries the Oracle database and exports it to txt text

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.