62,614
社区成员
发帖
与我相关
我的任务
分享
//上回给你回过一次,另一个帖上,代码如下,很粗糙,自己整理整理吧,细节方面的
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();
}
}
}
}