62,616
社区成员
发帖
与我相关
我的任务
分享
//有个地方写错了
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();
}
}
}
}
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();
}
}
}
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();
}
}
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;
}
}
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();
}
}
}
}