java文件讀寫+sql – JAVA編程語言程序開發技術文章

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 = "";//保存user_id

File file = new File("d:\\usere_login.txt");
FileWriter os = new FileWriter(file);
BufferedWriter usere_bw = new BufferedWriter(os);// 為user_id2創建一個文件

//File file1 = new File("d:\\ejob_id.txt");
//FileWriter os1 = new FileWriter(file1);
//BufferedWriter ejob_bw = new BufferedWriter(os1);// 為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");// 加載驅動程序

String url = "jdbc:mysql://10.10.10.11:21001/user";// URL指向要訪問的數據庫名message_old
String user = "root";// MySQL配置時的用戶名
String password = "@WSX#EDC6yhn";// MySQL配置時的密碼
Connection connection = DriverManager.getConnection(url, user,
password);// 連續數據庫
if (!connection.isClosed()) {
System.out.println("Succeeded connecting to the Database!");
}

Statement statement = connection.createStatement();// statement用來執行SQL語句

usere_bw.append("usere_id");
usere_bw.newLine();// 打印標題

//ejob_bw.append("ejob_id1");
//ejob_bw.newLine();// 打印標題

//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.executeQuery(sql);// 執行SQL語句並返回結果集

    while (rs.next()) {

    usere_bw.append(rs.getString(1));
    usere_bw.newLine();// 打印標題
   
    //ejob_bw.append(rs.getString(2));
    //ejob_bw.newLine();// 打印標題

    }
    rs.close();// 關閉果集
             }
 
 
             reader.close();
             in.close();

 

//}

System.out.println("數據生成結束");
connection.close();// 數據庫連接
usere_bw.flush();
// ejob_bw.flush();

} catch (ClassNotFoundException e) {
// 如果連接數據庫失敗就會報異常
System.out.println("Sorry,can`t find the Driver!");

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();
}

}
}
作者:gzh0222

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。