java读写操作
Posted 琦琦爸爸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java读写操作相关的知识,希望对你有一定的参考价值。
/*从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读写操作的主要内容,如果未能解决你的问题,请参考以下文章