sb.append(list.get(i)).append(separator);}
returnsb.toString().substring(0,sb.toString().length()-1);
}
publicstaticListdisAll(){//打印显示所有信息
//查询
Connectionconn=DBUtil.open();
Stringsql="selectid,name,frctionJava,fractionMath,fractionOSfromstudentTbl";
Statementstmt;
try{
stmt=conn.createStatement();
ResultSetrs=stmt.executeQuery(sql);
Listlist=newArrayList();
//遍历操作
while(rs.next()){
intid=rs.getInt
(1);
Stringname=rs.getString
(2);
doublea=rs.getDouble(3);
doubleb=rs.getDouble(4);
doublec=rs.getDouble(5);
Studentu=newStudent();
u.setId(id);
u.setName(name);
u.setFractionJava(a);
u.setFractionMath(b);
u.setFractionOS(c);
list.add(u);
}
conn.close();
returnlist;
}catch(SQLExceptione){
e.printStackTrace();
}
returnnull;
}
publicstaticvoidFindByID(longid,JLabellblNewLabel){//按ID查找
Connectionconn=DBUtil.open();
ResultSetrs=null;
try{
Stringsql="select*fromStudentTblwhereid=?
";
PreparedStatementsts=conn.prepareStatement(sql);
sts.setLong(1,id);
rs=sts.executeQuery();
while(rs.next()){
inti=rs.getInt
(1);
Stringname=rs.getString
(2);
doublea=rs.getDouble(3);
doubleb=rs.getDouble(4);
doublec=rs.getDouble(5);
lblNewLabel.setText("|"+i+"|"+name+"|"+a+"|"+b+"|"+c);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(rs.first()!
=true)
lblNewLabel.setText("~~~~~~~查无此人!
~~~~~~~~");
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
publicstaticvoidFindByName(Strings,JLabellblNewLabel){//按Name查找
Connectionconn=DBUtil.open();
ResultSetrs=null;
try{
Stringsql="select*fromStudentTblwherename=?
";
PreparedStatementsts=conn.prepareStatement(sql);
sts.setString(1,s);
rs=sts.executeQuery();
while(rs.next())
{
intid=rs.getInt
(1);
Stringname=rs.getString
(2);
doublea=rs.getDouble(3);
doubleb=rs.getDouble(4);
doublec=rs.getDouble(5);
lblNewLabel.setText("|"+id+"|"+name+"|"+a+"|"+b+"|"+c);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(rs.first()!
=true)
lblNewLabel.setText("~~~~~~~查无此人!
~~~~~~~~");
}catch(SQLExceptione)
{
e.printStackTrace();
}
}
}
publicstaticvoidDeleteByID(longid){//删除
Connectionconn=DBUtil.open();
PreparedStatementsts=null;
inta=0;
try{
sts=conn.prepareStatement("deletefromStudentTblwhereid=?
");
sts.setLong(1,id);
a=sts.executeUpdate();
if(a>0)
JOptionPane.showMessageDialog(null,"删除成功!
","WO",JOptionPane.INFORMATION_MESSAGE);
if(a==0)
JOptionPane.showMessageDialog(null,"无此人!
","WO",JOptionPane.INFORMATION_MESSAGE);
}catch(Exceptione){
e.printStackTrace();
}
DBUtil.close(conn);
}
publicstaticvoidSort(JTextAreata,intindex){//排序
//查询
Connectionconn=DBUtil.open();
Stringsql="selectid,name,frctionJava,fractionMath,fractionOSfromstudentTbl";
Statementstmt;
try{
stmt=conn.createStatement();
ResultSetrs=stmt.executeQuery(sql);
Listlist=newArrayList();
//遍历操作
while(rs.next()){
intid=rs.getInt
(1);
Stringname=rs.getString
(2);
doublea=rs.getDouble(3);
doubleb=rs.getDouble(4);
doublec=rs.getDouble(5);
Studentu=newStudent();
u.setId(id);
u.setName(name);
u.setFractionJava(a);
u.setFractionMath(b);
u.setFractionOS(c);
list.add(u);
}
Collections.sort(list,newComparator()
{
publicintcompare(Studento1,Studento2)
{
inti=1;
if(index==1)
{i=(int)(o2.getFractionJava()-o1.getFractionJava());}
if(index==2){
i=(int)(o2.getFractionMath()-o1.getFractionMath());
}
if(index==3){
i=(int)(o2.getFractionOS()-o1.getFractionOS());
}
returni;
}
});
Strings=listToString(list,'\n');
ta.setText(s);
}catch(SQLExceptione){
e.printStackTrace();
}
DBUtil.close(conn);
}
publicstaticvoidadd(Studentstu){//添加
Connectionconn=DBUtil.open();
PreparedStatementsts;
try{
sts=conn.prepareStatement("insertintoStudentTblvalues(?
?
?
?
?
)");
sts.setLong(1,stu.getId());
sts.setString(2,stu.getName());
sts.setDouble(3,stu.getFractionJava());
sts.setDouble(4,stu.getFractionMath());
sts.setDouble(5,stu.getFractionOS());
sts.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}
DBUtil.close(conn);}
publicstaticvoidUpdateStudent(Studentstu){//更新
Connectionconn=DBUtil.open();
try{
Stringsql="updatestudenttblsetname='"+stu.getName()+"',FrctionJava='"+stu.getFractionJava()+"',FractionMath='"+stu.getFractionMath()+"',FractionOS='"+stu.getFractionOS()+"'whereid='"+stu.getId()+"'";
PreparedStatementsts=conn.prepareStatement(sql);
sts.executeUpdate();
}catch(Exceptione){
e.printStackTrace();
}
}
}
classStudent
packagetest;
importjava.io.Serializable;
publicclassStudentimplementsSerializable{
privatestaticfinallongserialVersionUID=4558876142427402513L;
privateStringname;
privatelongid;
privatedoublefractionOS;
privatedoublefractionJava;
privatedoublefractionMath;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publiclonggetId(){
returnid;
}
publicvoidsetId(longid){
this.id=id;
}
publicdoublegetFractionOS(){
returnfractionOS;
}
publicvoidsetFractionOS(doublefractionOS){
this.fractionOS=fractionOS;
}
publicdoublegetFractionJava(){
returnfractionJava;
}
publicvoidsetFractionJava(doublefractionJava){
this.fractionJava=fractionJava;
}
publicdoublegetFractionMath(){
returnfractionMath;
}
publicvoidsetFractionMath(doublefractionMath){
this.fractionMath=fractionMath;
}
publicStringtoString(){
return(name+""+id+"Java:
"+fractionJava+
"Math:
"+fractionMath+"OS:
"+fractionOS);
}
}
classMyJFrame
packagetest;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjava.awt.BorderLayout;
importjava.awt.CardLayout;
importjava.awt.Insets;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjava.awt.Color;
importjavax.swing.SwingConstants;
importjavax.swing.JLabel;
importjava.awt.Font;
publicclassMyJFram