取值,写文件 我从数据库中取到值,怎么把它写到文件里面去? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 FileWriter fw = new FileWriter("xxxxoooo.txt");fw.write("你好");fw.close(); 我已经把数据取出来了(用JDBC取的),现在我想把取出的数据写到文件里面去。该怎么写? public void DBMessage(){ String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url="jdbc:sqlserver://localhost:3333;databaseName=aidb"; String us="sa"; String pw="GuoXiaoXian9207"; String sql="select * from ReportManage"; Connection conn=null; Statement stat=null; ResultSet rs=null; try { Class.forName(dbdriver); conn=DriverManager.getConnection(url, us, pw); stat=conn.createStatement(); rs=stat.executeQuery(sql); while(rs.next()){ System.out.println("id="+rs.getInt("id")); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }这是链接的数据 /** * 导出的文件 */public void ExportOutTableTest() { JFileChooser chooser = new JFileChooser(); chooser.showSaveDialog(this); File file = chooser.getSelectedFile(); String fileName = file.getName(); if (fileName != null && fileName.trim().length() > 0) { if (!(fileName.endsWith(".txt"))) { fileName = fileName.concat(".txt"); } } file = new File(file.getParent().concat(File.separator).concat(fileName)); if (file.exists()) { int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?"); if (i != JOptionPane.YES_OPTION) { return; } } try { file.createNewFile(); FileWriter fw = new FileWriter(file); fw.write(jtextFiledExport.getText()); fw.flush(); fw.close(); } catch (IOException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",JOptionPane.WARNING_MESSAGE); return; } }现在我怎么把取出的数据 放到文件里面去? package mySystem.myMail.actions.login;import java.io.FileWriter;import java.io.IOException;public class test1 { public static void main(String[] args) { FileWriter fw; try { fw = new FileWriter("xxxxoooo.txt"); fw.write("你好"); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}你把 你好 替换就行了……………… colId, sid, id, colCode, colName, colPid, colLevel, colType, colLenght:这是数据库的列名75122,2,6763,BQSJ,期末数|||实际数,0,75122,0,0,2010-07-26 10:51:54.193:这是数据库的数据75123,10,6763,BNNC,年初数,0,75123,0,0,2010-07-26 10:51:54.193,1,1,NULL,75124,6,6763,BQHB,合并数,0,75124,0,0,2010-07-26 10:51:54.193,1,0,NULL,0我怎么把它导出这种形式的TXT??? 恩,先导入列名colId, sid, id, colCode, colName, colPid, colLevel, colType, colLenght 加个换行接着到while(rs.next()){String str="";str+=rs.getInt("colId");//根据数据库情况而定Str+=",";String +=rs.getInt("sid");Str+=",";//此处省略n个字,楼主你懂的str =substr(0,str.length-1);str+="\r\t"//换行符//把String写入文件中}具体情况你修改一下,可能有部分拼写错误,木有环境 从数据库里面获取的可以string对象吧,那就创建文件流写入吧 public void DBMessage(){ String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url="jdbc:sqlserver://localhost:3333;databaseName=aidb"; String us="sa"; String pw="GuoXiaoXian9207"; String sql="select * from ReportManage"; Connection conn=null; Statement stat=null; ResultSet rs=null; try { Class.forName(dbdriver); conn=DriverManager.getConnection(url, us, pw); stat=conn.createStatement(); rs=stat.executeQuery(sql); while(rs.next()){ String str=""; str += rs.getInt("colId"); str += ","; str += rs.getInt("sid"); str += ","; str += rs.getInt("id"); str += ","; str += rs.getInt("colCode"); str += ","; str += rs.getString("colName"); str += ","; str += rs.getInt("colPid"); str += ","; str += rs.getInt("colLevel"); str += ","; str += rs.getInt("colType"); str += ","; str += rs.getInt("colLenght"); str += ","; str += rs.getString("colDate"); str += ","; str += rs.getInt("com_id"); str += ","; str += rs.getInt("display"); str += ","; str += rs.getInt("formula"); str += ","; str += rs.getInt("isText"); str += "\r\n"; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }public void ExportOutTableTest(String str) { JFileChooser chooser = new JFileChooser(); chooser.showSaveDialog(this); File file = chooser.getSelectedFile(); String fileName = file.getName(); if (fileName != null && fileName.trim().length() > 0) { if (!(fileName.endsWith(".txt"))) { fileName = fileName.concat(".txt"); } } file = new File(file.getParent().concat(File.separator).concat(fileName)); if (file.exists()) { int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?"); if (i != JOptionPane.YES_OPTION) { return; } } try { file.createNewFile(); FileWriter fw = new FileWriter(file); fw.write(str);//传进来的str fw.flush(); fw.close(); } catch (IOException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",JOptionPane.WARNING_MESSAGE); return; } }这样吗?我感觉还是有问题... 还有什么问题,我觉得你的大逻辑是没有什么问题的,都是小细节问题,自己调调还有你的DBMessage方法应该返回String吧,把你拼的那个字符串返回来,下面ExportOutTableTest的时候才能用到啊 思路用FileReader读出来String str = "";List<String[]> list = new ArrayList<String[]>();while(str=in.readline()!=null){//用split分开放入string[] strs 中,list.add(strs);}//接着你的数据都在list中了list.get(0)是你的字段名从1开始就是数据。 这样做不太灵活,要是我换一张表导的话又要改代码。有别的方法吗?用ResultSetMetaData怎么写?自动获取列的... 请教下jar文件到底是怎么打开的? socket通信问题 刚学java,遇到一个小问题!希望高手来给解决一下 问一个Swing问题! jdk1.2 我是一个初学者,这学期学JSP的,不过看书,有点抽象,又没有实例,大家能不能进来帮帮忙啊? 从网上了解到有一个java的数据挖掘的API,叫作JDM的。但是找不到啊!了解的高手给一个链接,好吗? 终于能发200分的帖子了,散分! 初学者报道(一) java语言程序设计第八版 基础篇 上的一道题 关于可变参数问题 求解杭电ACM PROBLEM 1001 JAVA
fw.write("你好");
fw.close();
String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:3333;databaseName=aidb";
String us="sa";
String pw="GuoXiaoXian9207";
String sql="select * from ReportManage";
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try {
Class.forName(dbdriver);
conn=DriverManager.getConnection(url, us, pw);
stat=conn.createStatement();
rs=stat.executeQuery(sql);
while(rs.next()){
System.out.println("id="+rs.getInt("id"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是链接的数据 /**
* 导出的文件
*/
public void ExportOutTableTest() {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(this);
File file = chooser.getSelectedFile();
String fileName = file.getName();
if (fileName != null && fileName.trim().length() > 0) {
if (!(fileName.endsWith(".txt"))) {
fileName = fileName.concat(".txt");
}
}
file = new File(file.getParent().concat(File.separator).concat(fileName));
if (file.exists()) {
int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?");
if (i != JOptionPane.YES_OPTION) {
return;
}
}
try {
file.createNewFile();
FileWriter fw = new FileWriter(file);
fw.write(jtextFiledExport.getText());
fw.flush();
fw.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",JOptionPane.WARNING_MESSAGE);
return;
}
}现在我怎么把取出的数据 放到文件里面去?
import java.io.IOException;public class test1 { public static void main(String[] args) {
FileWriter fw;
try {
fw = new FileWriter("xxxxoooo.txt");
fw.write("你好");
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
你把 你好 替换就行了………………
75122,2,6763,BQSJ,期末数|||实际数,0,75122,0,0,2010-07-26 10:51:54.193:这是数据库的数据
75123,10,6763,BNNC,年初数,0,75123,0,0,2010-07-26 10:51:54.193,1,1,NULL,
75124,6,6763,BQHB,合并数,0,75124,0,0,2010-07-26 10:51:54.193,1,0,NULL,0我怎么把它导出这种形式的TXT???
接着到while(rs.next()){String str="";
str+=rs.getInt("colId");//根据数据库情况而定
Str+=",";
String +=rs.getInt("sid");
Str+=",";
//此处省略n个字,楼主你懂的
str =substr(0,str.length-1);
str+="\r\t"//换行符
//把String写入文件中
}
具体情况你修改一下,可能有部分拼写错误,木有环境
String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:3333;databaseName=aidb";
String us="sa";
String pw="GuoXiaoXian9207";
String sql="select * from ReportManage";
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try {
Class.forName(dbdriver);
conn=DriverManager.getConnection(url, us, pw);
stat=conn.createStatement();
rs=stat.executeQuery(sql);
while(rs.next()){
String str="";
str += rs.getInt("colId");
str += ",";
str += rs.getInt("sid");
str += ",";
str += rs.getInt("id");
str += ",";
str += rs.getInt("colCode");
str += ",";
str += rs.getString("colName");
str += ",";
str += rs.getInt("colPid");
str += ",";
str += rs.getInt("colLevel");
str += ",";
str += rs.getInt("colType");
str += ",";
str += rs.getInt("colLenght");
str += ",";
str += rs.getString("colDate");
str += ",";
str += rs.getInt("com_id");
str += ",";
str += rs.getInt("display");
str += ",";
str += rs.getInt("formula");
str += ",";
str += rs.getInt("isText");
str += "\r\n";
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void ExportOutTableTest(String str) {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(this);
File file = chooser.getSelectedFile();
String fileName = file.getName();
if (fileName != null && fileName.trim().length() > 0) {
if (!(fileName.endsWith(".txt"))) {
fileName = fileName.concat(".txt");
}
}
file = new File(file.getParent().concat(File.separator).concat(fileName));
if (file.exists()) {
int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?");
if (i != JOptionPane.YES_OPTION) {
return;
}
}
try {
file.createNewFile();
FileWriter fw = new FileWriter(file);
fw.write(str);//传进来的str
fw.flush();
fw.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",JOptionPane.WARNING_MESSAGE);
return;
}
}
这样吗?我感觉还是有问题...
都是小细节问题,自己调调还有你的DBMessage方法应该返回String吧,
把你拼的那个字符串返回来,下面ExportOutTableTest的时候才能用到啊
用FileReader读出来
String str = "";
List<String[]> list = new ArrayList<String[]>();
while(str=in.readline()!=null){
//用split分开放入string[] strs 中,
list.add(strs);
}
//接着你的数据都在list中了
list.get(0)是你的字段名
从1开始就是数据。