码迷,mamicode.com
首页 > 编程语言 > 详细

java读写操作

时间:2017-12-06 11:30:33      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:cat   jdbc   new   from   get   input   file   void   sts   

/*从txt中读取文件参数到数据库中查询,将查询结果从新写入到文本文件中*/

 

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class we {
static String url;
static String name;
static String password;
static String id1;
static String temp ;
static{
Properties cfg=new Properties();
InputStream in=we.class.getClassLoader().getResourceAsStream("db.properties");
try {
cfg.load(in);
url=cfg.getProperty("url");
name=cfg.getProperty("name");
password=cfg.getProperty("password");
id1=cfg.getProperty("id1");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("testsql.txt",true));
BufferedReader br=new BufferedReader(new FileReader("read.txt"));
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url,name,password);
String sql="select id,name,phone from yrd_users where phone=?";
PreparedStatement psmt=conn.prepareStatement(sql);
String s=null;
while((s=br.readLine())!=null) {
System.out.println(s);
psmt.setString(1, s);
ResultSet res=psmt.executeQuery();
while(res.next()) {
String id=res.getString("id");
String name=res.getString("name");
String phone=res.getString("phone");
temp += phone+" "+ name +" "+id+"\r\n";
}
}
bw.write(temp);
conn.close();
bw.close();
br.close();
}
}

java读写操作

标签:cat   jdbc   new   from   get   input   file   void   sts   

原文地址:http://www.cnblogs.com/czb529514/p/7991669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!