Java file read/write + SQL

Source: Internet
Author: User

Package com. andyguo. test;


Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileWriter;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. LineNumberReader;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;


Public class JDBCtest {
Public static void main (String [] args) throws IOException,
ClassNotFoundException, SQLException {


// String user_id1 = ""; // save user_id


File file = new File ("d: \ usere_login.txt ");
FileWriter OS = new FileWriter (file );
BufferedWriter usere_bw = new BufferedWriter (OS); // create a file for user_id2


// File file1 = new File ("d: \ ejob_id.txt ");
// FileWriter os1 = new FileWriter (file1 );
// BufferedWriter ejob_bw = new BufferedWriter (os1); // create a file for user_id2

BufferedReader in = new BufferedReader (new InputStreamReader (
New FileInputStream ("d: \ usere_id.txt ")));
LineNumberReader reader = new LineNumberReader (in );


Try {


Class. forName ("com. mysql. jdbc. Driver"); // load the Driver


String url = "jdbc: mysql: // 10.10.10.11: 21001/user"; // The URL points to the name of the database to be accessed. message_old
String user = "root"; // user name configured for MySQL
String password = "@ WSX # EDC6yhn"; // password configured for MySQL
Connection connection = DriverManager. getConnection (url, user,
Password); // continuous Database
If (! Connection. isClosed ()){
System. out. println ("Succeeded connecting to the Database! ");
}


Statement statement = connection. createStatement (); // statement is used to execute SQL statements.


Usere_bw.append ("usere_id ");
Usere_bw.newLine (); // print the title


// Ejob_b1_append ("ejob_id1 ");
// Ejob_bw.newLine (); // print the title


// For (int I = 0; I <100; I ++ ){


// System. out. println ("I =" + I );


String SQL = null;


// String table_message = "user_message _" + I;

// String table_deegate = "web_appejob _" + I;
 
String s = reader. readLine ();
Int lines = 0;
 
While (s! = Null ){
 
Lines ++;

SQL = "SELECT user_login FROM web_user WHERE user_id =" + s;

System. out. println (SQL );

System. out. println (lines );

S = reader. readLine ();

ResultSet rs = statement.exe cuteQuery (SQL); // execute the SQL statement and return the result set


While (rs. next ()){


Usere_bw.append (rs. getString (1 ));
Usere_bw.newLine (); // print the title

// Ejob_b1_append (rs. getString (2 ));
// Ejob_bw.newLine (); // print the title


}
Rs. close (); // close the result set
}
 
 
Reader. close ();
In. close ();

 

//}


System. out. println ("data generation ends ");
Connection. close (); // database connection
Usere_b1_flush ();
// Ejob_b1_flush ();


} Catch (ClassNotFoundException e ){
// If the database connection fails, an exception is reported.
System. out. println ("Sorry, can't find the Driver! ");


E. printStackTrace ();


} Catch (SQLException e ){


E. printStackTrace ();


} Catch (Exception e ){


E. printStackTrace ();
}


}
}
Author: gzh0222

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.