package newexam;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import newexam.DAOAccess;
public class GetScore {
static String fileName = "d:/tiku/19考场汇总.crp";
static File outDir = new File("d:/tiku");
// 当前工作路径
static File absoluteDir = new File(outDir.getAbsolutePath());
static Connection con = null;
// 类静态方法,完成对数据库连接等初始化工作
static {
if (!absoluteDir.exists()) {
System.out.println("正在创建文件夹");
absoluteDir.mkdirs();
}
}
/**
* 从加密的成绩字符串有取出各单项成绩,形成一个成绩字符串数组
*
* @param tx
* 加密后成绩字符串
* @return 解密后的<code>成绩字符串数组</code>
* 其中分数列表为:单选、多选、判断、填空、Windows操作、Word、Excel、PPT等
*/
public static String[] getScoreByTx(String tx) {
char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
tx = tx.trim();
// 成绩字符串解密,密码0x79
byte b[] = tx.getBytes();
for (int i = 0; i < b.length; i++)
b[i] ^= 0x79;
// 定义一个分数数组,长度由Tx决定
char cHex[] = new char[b.length];
int k = 0;
// 将每个字节内容转换成分数,分数之间有个分隔符'c'
for (int i = 0; i < b.length; i++) {
byte x = b[i];
// 高四位没有使用
// cHex[k++] = hexChar[x >>> 4 & 0xf];
cHex[k++] = hexChar[x & 0xf];
}
String res = new String(cHex);
// 成绩字符串数组使用"c"分隔
return res.split("c");
}
/**
* @param score
* 存放考生成绩,其中第4,8,13位是成绩,其它位是学生的考号
* @return 考生的成绩
*/
public static int getTotal(String score) {
char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
byte b[] = score.getBytes();
for (int i = 0; i < b.length; i++)
b[i] ^= 0x79;
char cHex[] = new char[b.length];
int k = 0;
for (int i = 0; i < b.length; i++) {
byte x = b[i];
// 高四位没有使用
// cHex[k++] = hexChar[x >>> 4 & 0xf];
cHex[k++] = hexChar[x & 0xf];
}
String res = new String(cHex);
// 从成绩字符串中的第4,8,13位中取出成绩(其它位是该学生的考号)
String s = "" + res.charAt(4) + res.charAt(8) + res.charAt(13);
return Integer.parseInt(s);
}
/**
* 从数据库中读取成绩,包括单项成绩和总成绩,放入指定的文件中
*/
public static void getScore() throws Exception {
DAOAccess dao = new DAOAccess(fileName);
con = dao.getConnection();
Statement stat = con.createStatement();
String filename = "ScoreList.txt";
BufferedWriter out = new BufferedWriter(new FileWriter(new File(
absoluteDir, filename)));
String sql = "select * from kaoshengxinxi Order By sno";
ResultSet res = stat.executeQuery(sql);
out.write("************成 绩**************");
out.newLine();
out.write("学号\t\t\t考号\t姓名\t\t单选\t多选\t判断\t填空\tWin操作\tWord\tExcel\tPPt\tFrtPag\t总分");
out.newLine();
while (res.next()) {
out.write(res.getString("sno") + "\t");
out.write(res.getString("id") + "\t");
out.write(res.getString("name") + "\t");
String tempTx[] = getScoreByTx(res.getString("tx"));
for (String s : tempTx) {
out.write(s + ",\t");
}
int tempScore = getTotal(res.getString("score"));
out.write("" + tempScore);
out.newLine();
}
stat.close();
out.close();
dao.close();
System.out.println("成绩读取成功,请检查" + filename + "文件!");
}
public static void main(String args[]) throws Exception {
getScore();
}
}