高手一旦写出代码解决,200分及时全给-------数据库表与IO流的交互的问题.

刘彬彬 2008-04-25 07:40:17
从数据库的employee表(只需有名字,薪水,入职日期三列便可)中查出所有雇员信息,然后将所有记录存储到一个文本文件中(exployee.dat) 。Exployee.dat的格式如下:
总共有n个雇员的记录。
姓名 薪水 入职日期
1.张三 1000 2008-01-01
读取上面产生的数据文件中间部分的数据记录并写入到另一个文本文件中(exployee_bak.data)中


上面就是要写的程序的要求,如果高手能够写出代码实现,200分及时送出,并且全给.希望留言者用真正的代码说话,不要谈些理论的东西...
...全文
340 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpoysy 2008-04-26
  • 打赏
  • 举报
回复
原来是骗子哦,说好了再加100分的,^_^
huxinyu929 2008-04-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 huxinyu929 的回复:]
package simpletest;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.sql.Statement;


public class XLSCreate {

public static void main(String [] args)throws Exception{

ConnDB c = new ConnDB();
Statement stmt = c.getStatement();
String sql = "select * from employee";
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int i…
[/Quote]

连接数据库的类,,你自己写吧。。。

揭帖! 给分~~~
mike123hl 2008-04-25
  • 打赏
  • 举报
回复
顶一个先
hongke1490 2008-04-25
  • 打赏
  • 举报
回复
jdk1.5下编译通过,请修改数据库连接字符串。
hongke1490 2008-04-25
  • 打赏
  • 举报
回复
package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
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;
import java.util.ArrayList;
import java.util.List;

public class SaveDbData
{

private Connection conn = null;
private Statement stmt = null;
static
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
}

public SaveDbData()
{
try
{
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:db","user","pass");
stmt = conn.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}
}

public void closeDb()
{
try
{
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
SaveDbData test = new SaveDbData();
List<Person> list = new ArrayList<Person>();
test.readDataFromDb(list);
test.saveData(list, true);
test.readDataFromFile(list);
test.saveData(list,false);
test.closeDb();
}

public void saveData(List<Person> list,boolean hasHead)
{
BufferedWriter write = null;
Person p = null;
try
{
if(hasHead)
{
write = new BufferedWriter(new FileWriter("c:\\Exployee.dat"));
write.write("总共有"+list.size()+"个雇员的记录。\n");
write.write("姓名\t薪水\t入职日期\n");
}
else
{
write = new BufferedWriter(new FileWriter("c:\\exployee_bak.data"));
}
for(int i=0;i<list.size();i++)
{
p = list.get(i);
write.write((i+1)+"."+p.name+"\t"+p.salary+"\t"+p.date+"\n");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
write.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}

public void readDataFromFile(List<Person> list)
{
if(list != null)
list.clear();
else
list = new ArrayList<Person>();
Person p = null;
BufferedReader reader = null;
String line = "";
String[] person = null;
try
{
reader = new BufferedReader(new FileReader("c:\\Exployee.dat"));
reader.readLine();reader.readLine();
line = reader.readLine();
while(line != null && !"".equals(line))
{
p = new Person();
person = line.split("\t");
p.name = person[0].substring(person[0].indexOf(".")+1);
p.salary = Double.valueOf(person[1]);
p.date = person[2];
list.add(p);
line = reader.readLine();
}

}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}

public void readDataFromDb(List<Person> list)
{
if(list != null)
list.clear();
else
list = new ArrayList<Person>();
Person p = null;
ResultSet rs = null;
try
{
rs = stmt.executeQuery("select name,salary,date from employee");
while (rs.next())
{
p = new Person();
p.name = rs.getString(1);
p.salary = rs.getDouble(2);
p.date = rs.getString(3);
list.add(p);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}
}
}

class Person
{
public String name = "";
public double salary = 0.0D;
public String date = "";
}
anqini 2008-04-25
  • 打赏
  • 举报
回复

//有个地方写错了


package A;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class a {

private static Connection conn = null;

private static Statement s = null;

private static RandomAccessFile inout = null;

private static BufferedWriter out = null;
static {
try {
conn = DriverManager.getConnection("");
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) {
writeData();
readData();
}

private static void readData() {
try {

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\exployee_bak.data")));
inout.seek(0);
String s = inout.readLine();
System.out.println(s);
out.write(s);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
inout.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

private static void writeData() {
try {
s = conn.createStatement();
ResultSet rs = s.executeQuery("select * from employee");
int count = 0;
while (rs.next()) {
count++;
String name = rs.getString(1);//名字:
double gongzi = rs.getDouble(2);//工资
String date = rs.getString(3);//日期
inout = new RandomAccessFile("D:\\exployee.dat", "rw");
inout.writeChars(count + ". " + name + " " + String.valueOf(gongzi) + " " + date);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}

}
}

anqini 2008-04-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 michelecindy 的回复:]
拿anqini 的代码小修小补了下


JScript code
import java.io.*;
import java.sql.*;

public class BufferRead {
private static Connection conn = null;
private static Statement stmt = null;
private static RandomAccessFile inout = null;
private static BufferedWriter out = null;

static {
try {
conn = DriverManager.getConnection("");
}…
[/Quote]

我不知道你哪里改了。。。
莫情莫钱 2008-04-25
  • 打赏
  • 举报
回复
有高手在,学一下就好了
shili150 2008-04-25
  • 打赏
  • 举报
回复
哦噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢
huxinyu929 2008-04-25
  • 打赏
  • 举报
回复
package simpletest;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.sql.Statement;


public class XLSCreate {

public static void main(String [] args)throws Exception{

ConnDB c = new ConnDB();
Statement stmt = c.getStatement();
String sql = "select * from employee";
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int index = rs.getRow();
rs.beforeFirst();
String[][] a = new String[index][4];
for(int i=0;rs.next();i++){
a[i][0] = rs.getString(1)+"\t";
a[i][2] = rs.getString(2)+"\t";
a[i][2] = rs.getString(3)+"\t";
a[i][5] = "\n";
}
File srcFile = new File("d:\\sun\\1.dat");
FileOutputStream out = new FileOutputStream(srcFile);
for(int j=0;j<a.length;j++){
for(int k=0;k<a[j].length;k++){
String str = a[j][k];
byte[] b = str.getBytes();
out.write(b);
}
}
out.close();
}
}
Shine_Panda 2008-04-25
  • 打赏
  • 举报
回复
虽然我可以帮你一点。
但也不能全般帮你做完,有的还要靠你自己动手。
下面给你写了一部分代码。


package com.cao;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Employee {
//获得连接。
public Connection getConnection(){
Connection con = null;

try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs","sa","");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;


}
//读数据。
public List getAllInfo(){
List list = new ArrayList();
Connection con = this.getConnection();
try {
PreparedStatement pstm =con.prepareStatement("select * from employee");
ResultSet rs = pstm.executeQuery();

String oneEmployee ="";
while(rs.next()){
String name = rs.getString(1);
String wage = rs.getString(2);
String time = rs.getString(3);

oneEmployee = name+"\t"+wage+"\t"+time+"\n";

list.add(oneEmployee);
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




return list;
}
//写文件
public void writerFile(List list){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("Exployee.dat"));
for (int i = 0; i < list.size(); i++) {
bw.write(list.get(i).toString());
}
bw.close();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
刘彬彬 2008-04-25
  • 打赏
  • 举报
回复
谢谢大家的支持,今晚12点准时结贴.代码有用的,都有分!
michelecindy 2008-04-25
  • 打赏
  • 举报
回复
拿anqini 的代码小修小补了下


import java.io.*;
import java.sql.*;

public class BufferRead {
private static Connection conn = null;
private static Statement stmt = null;
private static RandomAccessFile inout = null;
private static BufferedWriter out = null;

static {
try {
conn = DriverManager.getConnection("");
} catch (SQLException e) {
e.printStackTrace();
}
}

private static void readDate() {
try {

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\exployee_bak.data")));
inout.seek(0);
String s = inout.readLine();
System.out.println(s);
out.write(s);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
inout.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

private static void writeDate() {
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from employee");
int count = 0;
while (rs.next()) {
count++;
int i = 1;
String name = rs.getString(i++);
double wages = rs.getDouble(i++);
String date = rs.getString(i++);//
inout = new RandomAccessFile("D:\\exployee.dat", "rw");
inout.writeChars(count + ". " + name + " " + String.valueOf(wages) + " " + date);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}
}

public static void main(String[] args) {
writeDate();
readDate();
}
}
胡矣 2008-04-25
  • 打赏
  • 举报
回复
exployee.dat
用XML是否能好一些?
kbryant 2008-04-25
  • 打赏
  • 举报
回复
up
cpoysy 2008-04-25
  • 打赏
  • 举报
回复
呵呵,楼主准备结贴给分吧?代码如下:

package txt;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class JavaControlTxt {

/**
* 用于存放员工信息
*/
List<Employee> employeeInfo=new ArrayList<Employee>();
/**
* 加入换行符
*/
final String strLine = System.getProperty("line.separator");
/**
* 操作的文件名
*/
final String fileName="employee.dat";
final String fileName_bak="employee_bak.dat";

public JavaControlTxt(){
//为了不写数据库代码,我这里就直接new Employee实例了,你用下面的Employee存放员工信息应该自己可以写数据库代码了吧?如果不会,再联系我.
Employee zhansan=new Employee("张三",3000,"2008/01/01");
Employee lisi=new Employee("李四",3200,"2007/01/04");
Employee wanwu=new Employee("王武",3005,"2008/07/01");
employeeInfo.add(zhansan);
employeeInfo.add(lisi);
employeeInfo.add(wanwu);
//以上查询员工信息数据库代码自己应该会写吧?
try {
FileWriter fw = new FileWriter(fileName);
fw.write("总共有"+employeeInfo.size()+"个雇员的记录。"+strLine);
fw.write("姓名\t薪水\t入职日期"+strLine);
for(int i=0;i<employeeInfo.size();i++){
String str=(i+1)+"."+employeeInfo.get(i).getUserName()+"\t"+employeeInfo.get(i).getPay()+"\t"+employeeInfo.get(i).getComeDate()+strLine;
fw.write(str);
}
fw.flush();
fw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
/**
* 以下读入前两行空信息
*/
br.readLine();
br.readLine();
String row=null;
FileWriter newFW=new FileWriter(fileName_bak);
while((row=br.readLine())!=null){
/**
* 将得到的一行转换为字符数据
*/
char[] c=row.toCharArray();
String _username=null;
int _pay=0;
String _comedate=null;
String s=null;//存放信息的临时变量
int x=1; //信号量,如果为1则表示存放_username(默认从第一个开始存放),如果为2则表示存放_pay,如果为3则表示存放_comedate
for(int i=0;i<c.length;i++){
if((int)c[i]!=9)
s=(s==null?"":s)+c[i];
else{
if(x==1){
_username=s;
s=null;
x++;
}else if(x==2){
_pay=Integer.parseInt(s);
s=null;
x++;
}
}
}
_comedate=s;

String str=(_username==null?"":_username)+"\t"+_pay+"\t"+(_comedate==null?"":_comedate)+strLine;
newFW.write(str);
}
newFW.flush();
newFW.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void main(String args[]){
new JavaControlTxt();
}
}

/**
* 这是一个bean文件,用于存放员工信息
* @author 陈平
*
*/
class Employee{

private String userName;
private int pay;
/**
* 为了简单起见,这里的日期我用String字符表示,其它转换成Date类型也不是难事,只是多加几行代码
*/
private String comeDate;

public Employee(String userName,int pay,String comeDate){
this.userName=userName;
this.pay=pay;
this.comeDate=comeDate;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getPay() {
return pay;
}
public void setPay(int pay) {
this.pay = pay;
}
public String getComeDate() {
return comeDate;
}
public void setComeDate(String comeDate) {
this.comeDate = comeDate;
}


}
psyl 2008-04-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 anqini 的回复:]
Java codepackageA;importjava.io.BufferedWriter;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.io.RandomAccessFile;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassa {privatestaticConnection conn=null;privatestatic…
[/Quote]

den88 2008-04-25
  • 打赏
  • 举报
回复
应谂何以,==
anqini 2008-04-25
  • 打赏
  • 举报
回复


package A;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class a {

private static Connection conn = null;

private static Statement s = null;

private static RandomAccessFile inout = null;

private static BufferedWriter out = null;
static {
try {
conn = DriverManager.getConnection("");
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) {
writeData();
readData();
}

private static void readData() {
try {

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\exployee_bak.data")));
inout.seek(0);
String s = inout.readLine();
System.out.println(s);
out.write(s);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
inout.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

private static void writeData() {
try {
s = conn.createStatement();
ResultSet rs = s.executeQuery("select * from employee");
int count = 0;
while (rs.next()) {
count++;
String name = rs.getString(1);
double gongzi = rs.getDouble(2);
String date = rs.getString(1);//
inout = new RandomAccessFile("D:\\exployee.dat", "rw");
inout.writeChars(count + ". " + name + " " + String.valueOf(gongzi) + " " + date);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}

}
}

anqini 2008-04-25
  • 打赏
  • 举报
回复
?
加载更多回复(8)

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧