import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConfig {
public static void main(String[] args) {
DBLink();
}
public static void DBLink(){
String driver = null;
String url = null;
String username = null;
String password = null;
/**************************************************************
*通过读取文件的配置链接数据库
**************************************************************/
File f = new File("config" + File.separator + "config.txt");
try {
//如果此目录文件存在就去读
if (f.exists()) {
// 读取
FileReader fr = new FileReader(f);
BufferedReader fb = new BufferedReader(fr);
String rDriver = fb.readLine();
driver = rDriver.substring(7);
String rUrl = fb.readLine();
url = rUrl.substring(4);
String rUserName = fb.readLine();
username = rUserName.substring(9);
String rPassword = fb.readLine();
password = rPassword.substring(9);
/* System.out.println(driver);
System.out.println(url);
System.out.println(username);
System.out.println(password);*/
/**************************************************************
*链接数据库读取数据,并将其写入文件
**************************************************************/
Connection con = null;
Statement sta = null;
ResultSet rs = null;
String sql = "select * from users";
try {
Class.forName(driver);
try {
//链接
con = DriverManager.getConnection(url, username, password);
sta = con.createStatement();
rs = sta.executeQuery(sql);
File f1 = new File("data" + File.separator + "users.txt");
f1.createNewFile();
FileWriter fw = new FileWriter(f1,true);
String dbStr = null;
while(rs.next()){
dbStr = "用户ID号:" + rs.getInt("u_id") + "用户名:" + rs.getString("u_name") + "年龄:" + rs.getInt("u_age") + "联系方式:" + rs.getString("u_phone") + "\r\n";
fw.write(dbStr);
fw.flush();
}
System.out.println("数据导出到文件完成!");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}else{
System.out.println("此文件不存在!系统将自动为您创建!");
//创建文件
f.createNewFile();
//写入数据
FileWriter fw = new FileWriter(f);
final String wDriver = "driver=oracle.jdbc.driver.OracleDriver\r\n";
String wUrl = "url=jdbc:oracle:thin:@localhost:1521:ORACLE9I\r\n";
String wUserName = "username=useinfo\r\n";
String wPassword = "password=useinfo\r\n";
fw.write(wDriver);
fw.write(wUrl);
fw.write(wUserName);
fw.write(wPassword);
fw.flush();
fw.close();
System.out.println("创建完成!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
最近下载更多
一个好人520 LV10
2021年9月29日
李经纬 LV1
2019年4月16日
Zero0006 LV1
2019年4月4日
1324488732 LV27
2018年12月25日
949132241 LV1
2018年11月13日
love123123 LV4
2018年9月19日
wyx065747 LV67
2017年6月28日
fanyiqiang LV14
2016年11月10日
Yuancc LV21
2016年7月22日

最近浏览