通过使用JDBC连接到数据库,查询出数据,然后将这些数据一条一条的写到一个文本文件中。一共两个类DBConnection和WriteToFile。具体代码如下:package Test;
import java.io.*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;import java.sql.Statement;
import java.sql.ResultSet;//创建连接数据库的类DBConnectionpublic class DBConnection{
public static void main(String args[]){
if (args.length < 3) {
System.out.println("Syntax:DBConnection [url][username][password]");
System.exit( -1);
}
DBConnection conn = new DBConnection(args[0],args[1],args[2]);
}public DBConnection(String url,String un,String pswd){
String URL = url;
String username = un;
String password = pswd;
String sql = "select column1,column2 from table";
String col1,col2;
String register,establish,connected,sqlstat,execute,result,queryresult; try{ register = "Step01.Registering JDBC Driver";
//将register写入到文件db.txt中
WriteToFile wtf = new WriteToFile(register,"g:\\db.txt");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
establish = "Step2.Establishing connection to :" + URL;
WriteToFile wtf1 = new WriteToFile(establish,"g:\\db.txt");
Connection con = DriverManager.getConnection(URL,username,password);
connected = " .Connected to Oracle!.";
WriteToFile wtf2 = new WriteToFile(connected,"g:\\db.txt");
sqlstat = "Step3.Creating SQL statement";
WriteToFile wtf3 = new WriteToFile(sqlstat,"g:\\db.txt");
Statement sta = con.createStatement();
execute = "Step4.Executing SQL statement.";
WriteToFile wtf4 = new WriteToFile(execute,"g:\\db.txt");
ResultSet query = sta.executeQuery(sql);
result = "Step5.Printing results.";
WriteToFile wtf5 = new WriteToFile(result,"g:\\db.txt");
//将查询出的结果写入到db.txt中
while(query.next()){
col1 = query.getString("column1");
col2 = query.getString("column2");
queryresult = " *"+ col1+""+col2;
WriteToFile wtf6 = new WriteToFile(queryresult,"g:\\db.txt");
}
System.out.println("Step6.Closing JDBC object.");
query.close();
sta.close();
con.close();
}catch(SQLException e){
System.out.println("The following error occurred:" + e.toString());
}}}//创建将查询到的记录写入文本文件的类WriteToFilepackage Test;
import java.io.*;public class WriteToFile {
public WriteToFile(String str,String path) {
try{
FileReader fr = new FileReader(str);
BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(path,true);
BufferedWriter bw = new BufferedWriter(fw); boolean eof = false;
int inChar = 0;
String stringIn ;
do{
//将字符串读入
stringIn = br.readLine();
if(inChar!=-1){
//将读入的字符串写入到db.txt中
bw.write(stringIn,1,stringIn.length());
}
}while(!eof);
br.close();
bw.close();
}catch (IOException e){
System.out.println("Error-- :" + e.toString());
} }
}
可是运行DBConnection后,总是说找不到文件,不知道怎么回事?我觉得是FileReader用的不是太对。那除了这个,还有哪个类是可以读入一行字符串的呢?请高手指教。谢谢!
import java.io.*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;import java.sql.Statement;
import java.sql.ResultSet;//创建连接数据库的类DBConnectionpublic class DBConnection{
public static void main(String args[]){
if (args.length < 3) {
System.out.println("Syntax:DBConnection [url][username][password]");
System.exit( -1);
}
DBConnection conn = new DBConnection(args[0],args[1],args[2]);
}public DBConnection(String url,String un,String pswd){
String URL = url;
String username = un;
String password = pswd;
String sql = "select column1,column2 from table";
String col1,col2;
String register,establish,connected,sqlstat,execute,result,queryresult; try{ register = "Step01.Registering JDBC Driver";
//将register写入到文件db.txt中
WriteToFile wtf = new WriteToFile(register,"g:\\db.txt");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
establish = "Step2.Establishing connection to :" + URL;
WriteToFile wtf1 = new WriteToFile(establish,"g:\\db.txt");
Connection con = DriverManager.getConnection(URL,username,password);
connected = " .Connected to Oracle!.";
WriteToFile wtf2 = new WriteToFile(connected,"g:\\db.txt");
sqlstat = "Step3.Creating SQL statement";
WriteToFile wtf3 = new WriteToFile(sqlstat,"g:\\db.txt");
Statement sta = con.createStatement();
execute = "Step4.Executing SQL statement.";
WriteToFile wtf4 = new WriteToFile(execute,"g:\\db.txt");
ResultSet query = sta.executeQuery(sql);
result = "Step5.Printing results.";
WriteToFile wtf5 = new WriteToFile(result,"g:\\db.txt");
//将查询出的结果写入到db.txt中
while(query.next()){
col1 = query.getString("column1");
col2 = query.getString("column2");
queryresult = " *"+ col1+""+col2;
WriteToFile wtf6 = new WriteToFile(queryresult,"g:\\db.txt");
}
System.out.println("Step6.Closing JDBC object.");
query.close();
sta.close();
con.close();
}catch(SQLException e){
System.out.println("The following error occurred:" + e.toString());
}}}//创建将查询到的记录写入文本文件的类WriteToFilepackage Test;
import java.io.*;public class WriteToFile {
public WriteToFile(String str,String path) {
try{
FileReader fr = new FileReader(str);
BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(path,true);
BufferedWriter bw = new BufferedWriter(fw); boolean eof = false;
int inChar = 0;
String stringIn ;
do{
//将字符串读入
stringIn = br.readLine();
if(inChar!=-1){
//将读入的字符串写入到db.txt中
bw.write(stringIn,1,stringIn.length());
}
}while(!eof);
br.close();
bw.close();
}catch (IOException e){
System.out.println("Error-- :" + e.toString());
} }
}
可是运行DBConnection后,总是说找不到文件,不知道怎么回事?我觉得是FileReader用的不是太对。那除了这个,还有哪个类是可以读入一行字符串的呢?请高手指教。谢谢!
解决方案 »
- 求最大公约数
- 如何将BigDecimal格式化为科学计数法的字符串(没有精度的损失)?
- 如何取得执行sql后的表头?
- 哪里有JGraph的资料啊???
- 高清晰电影免费下载http://free.dlmovie.net/movie/freemovie.asp?userid=shenjr
- 一个关于swing的问题
- 大家看看这个这个网站!!www.66wz.com 用到j2ee吗?
- 请问我的java小应用程序,在ie里面无论中文英文都显示为方框,是怎么回事啊!!!!高分
- 兄弟们给一个JAVA树型结构比较优化的算法吧
- 提问:关于JAVA处理网络音频传输的问题????
- 取小数点后4位?请高手帮忙写一段代码。。
- JMX:The MBean class could not be loaded...救命
String stringIn ;
还有
while(!eof);
根本就没赋值,怎么能运行起来呢??
public WriteToFile(String str, String path) {
try {
FileWriter fw = new FileWriter(new File(path), true);
BufferedWriter bw = new BufferedWriter(fw); // 将读入的字符串写入到db.txt中
bw.write(str, 0, str.length());
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
System.out.println("Error-- :" + e.toString());
}
}
}
/*
* 我的测试类
/*package test;public class Client { public static void main(String[] args) {
new WriteToFile("temp", "g:\\db.txt");
new WriteToFile("temp2", "g:\\db.txt");
}}