Eclipse中如何将从数据库中查询到的结果集rs保存到文件中
以下是代码和运行结果,如何把运行的结果集写入到文件中?packagecom.neusoft.chatroom.service.start;importjava.sql.*...
以下是代码和运行结果,如何把运行的结果集写入到文件中?
package com.neusoft.chatroom.service.start;
import java.sql.*;
import java.io.*;
import com.mysql.jdbc.ResultSetImpl;
public class MysqlJdbc {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){e.printStackTrace();}
try{
String str=new String();
String thisLine;
Connection connect=DriverManager.getConnection(
"jdbc:mysql://172.19.28.89/chat","root","root");
Statement stmt=connect.createStatement();
ResultSet rs=stmt.executeQuery("select * from message");
while(rs.next()){
System.out.println(rs.getString("time"));
System.out.print(rs.getString("get_id"));
System.out.print("对");
System.out.print(rs.getString("send_id"));
System.out.print("说:");
System.out.println(rs.getString("message"));
}
}
catch(Exception e){e.printStackTrace();}
}
} 展开
package com.neusoft.chatroom.service.start;
import java.sql.*;
import java.io.*;
import com.mysql.jdbc.ResultSetImpl;
public class MysqlJdbc {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){e.printStackTrace();}
try{
String str=new String();
String thisLine;
Connection connect=DriverManager.getConnection(
"jdbc:mysql://172.19.28.89/chat","root","root");
Statement stmt=connect.createStatement();
ResultSet rs=stmt.executeQuery("select * from message");
while(rs.next()){
System.out.println(rs.getString("time"));
System.out.print(rs.getString("get_id"));
System.out.print("对");
System.out.print(rs.getString("send_id"));
System.out.print("说:");
System.out.println(rs.getString("message"));
}
}
catch(Exception e){e.printStackTrace();}
}
} 展开
2个回答
展开全部
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* java读取数据库内容并存放到文件中
* id作为文件名,相同的id只能创建一个文件
* 有重复文件名就向文件中续写,id不存在就新建
*
* 写入文件换行用fw.write("\r\n");
* 或者fw.write("\n");
* @author young
*
*/
public class JavaToSQLTest {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter pw = null;
FileWriter fw = null;
// 定义数据库驱动
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// 数据库连接URL
String url = "jdbc:sqlserver://localhost:1433;DatabaseName = soft";
Connection conn = null;
String id;
String fname, lname;
try {
// pw.println("emp_id\t\tfname\t\tlname");
// pw.println("------\t\t------\t\t------");
// 加载数据库驱动
Class.forName(driver);
// 创建数据库连接
conn = DriverManager.getConnection(url, "sa", "1234");
// 创建预编译SQL对象
PreparedStatement ps = conn
.prepareStatement("select emp_id, fname, lname from emps");
// 执行SQL,获取结果集rs
ResultSet rs = ps.executeQuery();
// 处理结果集
while (rs.next()) {
id = rs.getString("emp_id");
fname = rs.getString("fname");
lname = rs.getString("lname");
String filename = id + ".txt";
// 关联文件
File file = new File(filename);
if(!file.exists()){
// 判断文件不存在就new新文件,写数据
try {
file.createNewFile();
// java IO流和文件关联
pw = new PrintWriter(file);
pw.print(id + "\t");
pw.print(fname + "\t\t");
pw.print(lname);
pw.println();
pw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
// 判断文件存在,就以FileWriter文件追加的方式写文件
try {
fw = new FileWriter(filename,true);
fw.write(id + "\t");
fw.write(fname + "\t\t");
fw.write(lname);
fw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("加载数据库失败");
System.exit(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接错误");
System.exit(1);
} finally {
if (conn != null) {
try {
// 关闭数据库连接
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pw != null) {
// 关闭IO流
pw.close();
}
if(fw != null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
展开全部
用FileInputStream类
追问
具体怎么用呢
追答
try
{
String str =
"1234567890";
FileOutputStream fos =
new FileOutputStream(new File("pathname"));
fos.write(str.getBytes());
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询