package com.xaccp;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import java.io.*;
import java.awt.*;
public class Frame1 extends JFrame {
public Frame1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
lblInfo.setText("学生信息");
lblInfo.setBounds(new Rectangle(21, 47, 72, 24));
jScrollPane1.setBounds(new Rectangle(19, 72, 246, 155));
btnSave.setBounds(new Rectangle(20, 252, 110, 26));
btnSave.setEnabled(false);
btnSave.setText("保存成文件");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnSave_actionPerformed(e);
}
});
btnRead.setBounds(new Rectangle(147, 251, 74, 26));
btnRead.setText("读取");
btnRead.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnRead_actionPerformed(e);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("学生成绩管理");
this.getContentPane().add(lblInfo);
this.getContentPane().add(jScrollPane1);
this.getContentPane().add(btnSave);
this.getContentPane().add(btnRead);
jScrollPane1.getViewport().add(lstInfo);
this.setBounds(100, 200, 300, 350);
this.setVisible(true);
}
public static void main(String[] args) {
new Frame1();
}
Vector v;
JLabel lblInfo = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JList lstInfo = new JList();
JButton btnSave = new JButton();
JButton btnRead = new JButton();
public void btnSave_actionPerformed(ActionEvent e) {
try {
File f = new File("d:\\aa.txt");
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < v.size(); i++) {
bw.write(v.get(i).toString()+"\r\n");
}
javax.swing.JOptionPane.showMessageDialog(this,
"文件" + f.getName() + "保存成功!");
bw.close();
fw.close();
} catch (HeadlessException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void btnRead_actionPerformed(ActionEvent e) {
try {
DBManager dbm = new DBManager();
String strSql="select * from student";
ResultSet rs=dbm.executeQuerry(strSql);
v=new Vector();
while(rs.next()){
v.add("姓名:"+rs.getString("stuName"));
v.add("性别:"+rs.getString("stuSex"));
v.add("成绩:"+rs.getString("stuScore"));
v.add(" ");
}
this.lstInfo.setListData(v);
btnRead.setEnabled(false);
this.btnSave.setEnabled(true);
rs.close();
dbm.closeConnection();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

浙公网安备 33010602011771号