今天做了一下上周日参加的ITAT大赛的未做完的试题,一是用PreparedStatement处理数据库的问题,我已经做出来了,代码贴一哈,在这里秀秀,欢迎拍砖!
/**
* @(#)T6.java
*
*
* @author Xie Xiaojin
* @version 1.00 2009/11/3
*/
import java.sql.*;
public class T6 {
private String[][] values = {
{"200731102075", "Ma Junjun", "M", "20", "Computer Science & Technoloty 072", "83"},
{"200731102076", "Xie Xiaojin", "M", "21", "Computer Science & Technoloty 072", "84"},
{"200731102077", "Cai Fan", "M", "21", "Computer Science & Technoloty 072", "85"},
{"200731102078", "Xia Haibin", "M", "21", "Computer Science & Technoloty 072", "86"},
{"200731102079", "Wu Dianyu", "M", "22", "Computer Science & Technoloty 072", "87"}
};
public T6() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "sxwxujn89313!@#");
PreparedStatement pstmt = con.prepareStatement("update Student_1 set name = ?, sex = ?, class = ? where no = ?; ");
try{
for(int i = 0; i < values.length; i++){
pstmt.setString(1, values[i][1]);
pstmt.setString(2, values[i][2]);
pstmt.setString(3, values[i][4]);
pstmt.setString(4, values[i][0]);
pstmt.executeUpdate();
}
}catch(SQLException ex){
System.err.println("不能修改数据!");
}
ResultSet rs = pstmt.executeQuery("select * from Student_1; ");
while(rs.next()){
System.out.println(rs.getString(1) + "/t" + rs.getString(2) + "/t" + rs.getString(3)+ "/t" + rs.getString(4)+
"/t" + rs.getString(5)+ "/t" + rs.getString(6) );
}
rs.close();
pstmt.close();
con.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main (String[] args) {
new T6();
}
}
另一个是Serializable序列化问题,也做出来了,不过都是大赛完毕之后做出来的,代码也贴在这儿
/**
* @(#)T3.java
*
*
* @author Xie Xiaojin
* @version 1.00 2009/11/3
*/
import java.io.*;
public class T3 {
private ObjectOutputStream oos;
private ObjectInputStream ois;
private Student student1 = new Student(200731102076L, "Xie Xiaojin", "Computer Science & Technology School", 84);
public T3() {
try{
oos = new ObjectOutputStream(new FileOutputStream("Student.ser"));
ois = new ObjectInputStream(new FileInputStream("Student.ser"));
oos.writeObject(student1);
oos.flush();
Student student2 = (Student)(ois.readObject());
System.out.println(student2);
}catch(IOException ex){
ex.printStackTrace();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}finally{
try{
oos.close();
ois.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public static void main (String[] args) {
new T3();
}
}
class Student implements Serializable{
private long id;
private String name;
private String depart;
private int score;
public Student(){
this(200731102076L, "Xie Xiaojin", "Computer Science & Technology School", 84);
}
public Student(long id, String name, String depart, int score){
this.id = id;
this.name = name;
this.depart = depart;
this.score = score;
}
public long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getDepart(){
return depart;
}
public void setDepart(String depart){
this.depart = depart;
}
public int getScore(){
return score;
}
public void setScore(int score){
this.score = score;
}
public String toString(){
return id + "/t" + name + "/t" + depart + "/t" + score;
}
}
题目要求是要序列化属性,而不序列化方法,我找来找去都找不到相关的例子或者文字,只好在CSDN上问了一句,好心人说根本没有序列化方法这种说法,我不知道,且悬之在这儿!!下面我又想这样多不好啊,要是能序列化到XML文档里不是更好吗,既可读性高有能处理大量数据,好处多多,于是我又在书上翻来翻去,在网上找来找去,终于找到了一个比较完美的解决办法,而且都试着学习那些很好的例子,我就将他们的博客转帖了过来,然后又有个org.apache.crimson.tree包,也终于在http://xml.apache.org/crimson/上找到了,然后将解压缩得到的crimson.jar包复制到java/lib/ext/目录下,试试,果然能行了,真是个不错的解决方案啊,happy!