当前位置: 代码迷 >> SQL >> java资料读写+sql
  详细解决方案

java资料读写+sql

热度:85   发布时间:2016-05-05 15:15:12.0
java文件读写+sql
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();
}


}
}
  相关解决方案