[java]代码库
/**
* @(#)StudentSystem.java
*
*
* @author
* @version 1.00 2012/10/31
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.JTableHeader;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class AddForm extends JFrame implements ActionListener {
JLabel labName = new JLabel("学号:");
JLabel labDate = new JLabel("出生日期:");
JLabel labScore = new JLabel("成绩:");
JTextField txtName = new JTextField(20);
JTextField txtDate = new JTextField(18);
JTextField txtScore = new JTextField(20);
JButton btnOk = new JButton("确定");
JButton btnClear = new JButton("清空");
JPanel pan = new JPanel();
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
Connection cnn;
Statement stm;
ResultSet rs;
AddForm() {
super("添加数据");
setSize(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pan.setBorder(BorderFactory.createEtchedBorder());
pan1.add(labName);
pan1.add(txtName);
pan2.add(labDate);
pan2.add(txtDate);
pan3.add(labScore);
pan3.add(txtScore);
pan4.add(btnOk);
pan4.add(btnClear);
pan.setLayout(new GridLayout(3, 1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
getContentPane().add(pan, "Center");
getContentPane().add(pan4, "South");
btnOk.addActionListener(this);
btnClear.addActionListener(this);
setVisible(true);
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnClear) {
txtName.setText("");
txtDate.setText("");
txtScore.setText("");
txtName.requestFocus();
} else if (ae.getSource() == btnOk) {
String strName = txtName.getText();
String strDate = txtDate.getText();
String strScore = txtScore.getText();
if (strName.equals(""))
JOptionPane.showMessageDialog(this, "学号不能为空!", "警告",
JOptionPane.ERROR_MESSAGE);
else if (strDate.equals(""))
JOptionPane.showMessageDialog(this, "出生日期不能为空!", "警告",
JOptionPane.ERROR_MESSAGE);
else if (strScore.equals(""))
JOptionPane.showMessageDialog(this, "成绩不能为空!", "警告",
JOptionPane.ERROR_MESSAGE);
else {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
cnn = DriverManager.getConnection("Jdbc:Odbc:MyDB");
stm = cnn.createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
rs = stm.executeQuery("select * from 成绩表 where 学号='"
+ strName + "'");
if (rs.next()) {
JOptionPane.showMessageDialog(this, "对不起,该成绩信息已存在!");
} else // 否则插入记录
{
// System.out.println("insert into 成绩表 values('"+strName+"',#"+strDate+"#,"+strScore+")");
stm.executeUpdate("insert into 成绩表 values('" + strName
+ "','" + strDate + "'," + strScore + ")");
JOptionPane.showMessageDialog(null, "记录已经成功添加!");
}
// 断开连接
stm.close();
cnn.close();
} catch (SQLException ex) {
System.out.println("SQLException:" + ex.getMessage());
}
}
}
}
public static void main(String[] args) {
new AddForm();
}
}
class BrowseForm extends JFrame {
String[] str = { "学号", "出生日期", "成绩" };
Object[][] data;
JTable table;
JTableHeader head;
JScrollPane jsp;
Connection conn;
Statement stmt;
ResultSet rs;
BrowseForm() {
super("浏览数据");
setSize(400, 300);
int i = 0, j = 0;
int row;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
stmt = conn.createStatement();
rs = stmt.executeQuery("select COUNT(*) as a from 成绩表");
rs.next();
row = rs.getInt("a");
rs.close();
data = new Object[row][3];
rs = stmt.executeQuery("select * from 成绩表");
while (rs.next()) {
data[i][j++] = rs.getString("学号");
data[i][j++] = rs.getDate("出生日期");
data[i][j] = new Integer(rs.getInt("成绩"));
i++;
j = 0;
}
table = new JTable(data, str);
head = table.getTableHeader();
jsp = new JScrollPane(table);
getContentPane().add(head, "North");
getContentPane().add(jsp, "Center");
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new BrowseForm();
}
}
class DeleteForm extends JFrame implements ActionListener {
JLabel labName = new JLabel("学号:");
JLabel labDate = new JLabel("出生日期:");
JLabel labScore = new JLabel("成绩:");
JTextField txtName = new JTextField(20);
JTextField txtDate = new JTextField(18);
JTextField txtScore = new JTextField(20);
JButton btnDel = new JButton("删除");
JButton btnCancel = new JButton("取消");
JButton btnQuery = new JButton("查询");
JPanel pan = new JPanel();
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
Connection cnn;
Statement stm;
ResultSet rs;
DeleteForm() {
super("删除数据");
setSize(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pan.setBorder(BorderFactory.createEtchedBorder());
pan1.add(labName);
pan1.add(txtName);
pan2.add(labDate);
pan2.add(txtDate);
pan3.add(labScore);
pan3.add(txtScore);
pan4.add(btnQuery);
pan4.add(btnDel);
pan4.add(btnCancel);
pan.setLayout(new GridLayout(3, 1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
getContentPane().add(pan, "Center");
getContentPane().add(pan4, "South");
btnQuery.addActionListener(this);
btnDel.addActionListener(this);
btnCancel.addActionListener(this);
btnDel.setEnabled(false);
txtDate.setEditable(false);
txtScore.setEditable(false);
setVisible(true);
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae) {
/*
* if(ae.getSource()==btnCancel){ try { if(stm!=null) stm.close();
* if(cnn!=null) cnn.close(); } catch (SQLException ex) {
* ex.printStackTrace(); } this.dispose(); } else
* if(ae.getSource()==btnQuery){ try{
* Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
* cnn=DriverManager.getConnection("jdbc:odbc:MyDB","","");
* stm=cnn.createStatement(); rs=stm.executeQuery("select * from 成绩表
* where 学号 ='"+txtName.getText()+"'"); if(rs.next()){
* txtName.setText(rs.getString("学号"));
* txtDate.setText(rs.getDate("出生日期").toString()); txtScore.setText(new
* Integer(rs.getInt("成绩")).toString ()); btnDel.setEnabled(true);
* }else{ JOptionPane.showMessageDialog(this,"不存在该记录!");
* btnDel.setEnabled(false); txtName.setText(""); txtScore.setText("");
* txtDate.setText(""); } }catch(Exception e){ e.printStackTrace(); }
* }else if(ae.getSource()==btnDel){ try {
* if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog
* (this,"确定要删除该记录?","信息",JOptionPane.YES_NO_OPTION)){
* stm.executeUpdate("delete from 成绩表 where 学号
* ='"+txtName.getText()+"'"); btnDel.setEnabled(false);
* txtName.setText(""); txtScore.setText(""); txtDate.setText(""); } }
* catch (SQLException ex) { ex.printStackTrace(); } }
*/
}
public static void main(String[] args) {
new DeleteForm();
}
}
class LoginForm extends JFrame implements ActionListener {
JLabel labName = new JLabel("姓名");
JLabel labPwd = new JLabel("密码");
JTextField txtName = new JTextField(20);
JPasswordField txtPwd = new JPasswordField(20);
JButton btnOk = new JButton("确定");
JButton btnCancel = new JButton("取消");
JPanel pan = new JPanel();
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
Connection cnn;
Statement stm;
ResultSet rs;
LoginForm() {
super("用户登录");
setSize(300, 200);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pan.setBorder(BorderFactory.createTitledBorder("登录"));
pan.setLayout(new GridLayout(2, 1));
pan1.add(labName);
pan1.add(txtName);
pan2.add(labPwd);
pan2.add(txtPwd);
pan.add(pan1);
pan.add(pan2);
pan3.add(btnOk);
pan3.add(btnCancel);
pan4.add(pan);
getContentPane().add(pan4, "Center");
getContentPane().add(pan3, "South");
txtName.addActionListener(this);
txtPwd.addActionListener(this);
btnOk.addActionListener(this);
btnCancel.addActionListener(this);
setVisible(true);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
cnn = DriverManager.getConnection("Jdbc:Odbc:MyDB");
stm = cnn.createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
}
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == txtName)
txtPwd.requestFocus();
else if (ae.getSource() == txtPwd)
btnOk.requestFocus();
else if (ae.getSource() == btnCancel) {
txtName.setText("");
txtPwd.setText("");
txtName.requestFocus();
} else if (ae.getSource() == btnOk) {
String str = "select * from 用户表 where 用户名='" + txtName.getText()
+ "'and 密码='" + new String(txtPwd.getPassword()) + "'";
try {
rs = stm.executeQuery(str);
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
if (rs.next()) {
JOptionPane.showMessageDialog(this, "验证通过!", "信息",
JOptionPane.INFORMATION_MESSAGE);
rs.close();
stm.close();
cnn.close();
new MainForm().setVisible(true);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码不正确!", "信息",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new LoginForm();
}
}
class ModifyForm extends JFrame implements ActionListener {
JLabel labName = new JLabel("学号:");
JLabel labDate = new JLabel("出生日期:");
JLabel labScore = new JLabel("成绩:");
JTextField txtName = new JTextField(20);
JTextField txtDate = new JTextField(18);
JTextField txtScore = new JTextField(20);
JButton btnModify = new JButton("修改");
JButton btnCancel = new JButton("取消");
JButton btnQuery = new JButton("查询");
JPanel pan = new JPanel();
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
Connection cnn;
Statement stm;
ResultSet rs;
ModifyForm() {
super("修改数据");
setSize(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pan.setBorder(BorderFactory.createEtchedBorder());
pan1.add(labName);
pan1.add(txtName);
pan2.add(labDate);
pan2.add(txtDate);
pan3.add(labScore);
pan3.add(txtScore);
pan4.add(btnQuery);
pan4.add(btnModify);
pan4.add(btnCancel);
pan.setLayout(new GridLayout(3, 1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
getContentPane().add(pan, "Center");
getContentPane().add(pan4, "South");
btnQuery.addActionListener(this);
btnModify.addActionListener(this);
btnCancel.addActionListener(this);
btnModify.setEnabled(false);
txtDate.setEditable(false);
txtScore.setEditable(false);
setVisible(true);
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnCancel) {
try {
if (stm != null)
stm.close();
if (cnn != null)
cnn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
this.dispose();
} else if (ae.getSource() == btnQuery) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
stm = cnn.createStatement();
rs = stm.executeQuery("select * from 成绩表 where 学号='"
+ txtName.getText() + "'");
if (rs.next()) {
txtName.setText(rs.getString("学号"));
txtScore.setText(new Integer(rs.getInt("成绩")).toString());
txtDate.setText(rs.getDate("出生日期").toString());
btnModify.setEnabled(true);
txtDate.setEditable(true);
txtScore.setEditable(true);
} else {
JOptionPane.showMessageDialog(this, "不存在该记录!");
btnModify.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (ae.getSource() == btnModify) {
try {
System.out.println("Update 成绩表 set 出生日期=#" + txtDate.getText()
+ "#,成绩=" + txtScore.getText() + " where 学号='"
+ txtName.getText() + "'");
stm.executeUpdate("Update 成绩表 set 出生日期=#" + txtDate.getText()
+ "#,成绩=" + txtScore.getText() + " where 学号='"
+ txtName.getText() + "'");
JOptionPane.showMessageDialog(this, "记录修改完毕!");
btnModify.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
stm.close();
cnn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ModifyForm();
}
}
class NumberQueryForm extends JFrame implements ActionListener {
JLabel labName = new JLabel("学号:");
JLabel labDate = new JLabel("出生日期:");
JLabel labScore = new JLabel("成绩:");
JTextField txtName = new JTextField(20);
JTextField txtDate = new JTextField(18);
JTextField txtScore = new JTextField(20);
JButton btnCancel = new JButton("取消");
JButton btnQuery = new JButton("查询");
JPanel pan = new JPanel();
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
Connection cnn;
Statement stm;
ResultSet rs;
NumberQueryForm() {
super("按学号查询");
setSize(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pan.setBorder(BorderFactory.createEtchedBorder());
pan1.add(labName);
pan1.add(txtName);
pan2.add(labDate);
pan2.add(txtDate);
pan3.add(labScore);
pan3.add(txtScore);
pan4.add(btnQuery);
pan4.add(btnCancel);
pan.setLayout(new GridLayout(3, 1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
getContentPane().add(pan, "Center");
getContentPane().add(pan4, "South");
btnQuery.addActionListener(this);
btnCancel.addActionListener(this);
txtDate.setEditable(false);
txtScore.setEditable(false);
setVisible(true);
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnCancel) {
try {
if (stm != null)
stm.close();
if (cnn != null)
cnn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
this.dispose();
} else if (ae.getSource() == btnQuery) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
stm = cnn.createStatement();
rs = stm.executeQuery("select * from 成绩表 where 学号='"
+ txtName.getText() + "'");
if (rs.next()) {
txtName.setText(rs.getString("学号"));
txtScore.setText(new Integer(rs.getInt("成绩")).toString());
txtDate.setText(rs.getDate("出生日期").toString());
} else {
JOptionPane.showMessageDialog(this, "不存在该记录!");
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtName.requestFocus();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new NumberQueryForm();
}
}
class ScoreQueryForm extends JFrame implements ActionListener {
JLabel labScore = new JLabel("请输入成绩:");
JTextField txtScore = new JTextField(10);
JButton btnQuery = new JButton("查询");
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
String[] str = { "学号", "出生日期", "成绩" };
Object[][] data = new Object[10][3];
JTable table = new JTable(data, str);
JTableHeader head = table.getTableHeader();
JScrollPane jsp = new JScrollPane(table);
Connection conn;
Statement stmt;
ResultSet rs;
ScoreQueryForm() {
super("按成绩查询");
setSize(400, 300);
pan1.add(labScore);
pan1.add(txtScore);
pan1.add(btnQuery);
getContentPane().add(pan1, "North");
table = new JTable(data, str);
pan2.setLayout(new BorderLayout());
head = table.getTableHeader();
jsp = new JScrollPane(table);
pan2.add(head, "North");
pan2.add(jsp, "Center");
getContentPane().add(pan2, "Center");
btnQuery.addActionListener(this);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnQuery) {
int i, j, row;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
stmt = conn.createStatement();
rs = stmt
.executeQuery("select COUNT(*) as rowcount from 成绩表 where 成绩="
+ txtScore.getText());
rs.next();
row = rs.getInt("rowcount");
rs.close();
data = new Object[row][3];
rs = stmt.executeQuery("select * from 成绩表 where 成绩="
+ txtScore.getText());
i = 0;
j = 0;
while (rs.next()) {
data[i][j++] = rs.getString("学号");
data[i][j++] = rs.getDate("出生日期");
data[i][j] = new Integer(rs.getInt("成绩"));
i++;
j = 0;
}
pan2.removeAll();
getContentPane().remove(pan2);
table = new JTable(data, str);
pan2.setLayout(new BorderLayout());
head = table.getTableHeader();
jsp = new JScrollPane(table);
pan2.add(head, "North");
pan2.add(jsp, "Center");
getContentPane().add(pan2, "Center");
this.validate();
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ScoreQueryForm();
}
}
class MyPanel extends JPanel {
Image img = Toolkit.getDefaultToolkit().getImage("c:/a.jpg");
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
class MainForm extends JFrame implements ActionListener {
JMenu mSystem = new JMenu("系统");
JMenuItem mExit = new JMenuItem("退出");
JMenu mOperate = new JMenu("数据操作");
JMenuItem mAdd = new JMenuItem("添加");
JMenuItem mDel = new JMenuItem("删除");
JMenuItem mModify = new JMenuItem("修改");
JMenuItem mBrowse = new JMenuItem("浏览");
JMenu mQuery = new JMenu("查询");
JMenuItem mNumber = new JMenuItem("按学号查询");
JMenuItem mScore = new JMenuItem("按成绩查询");
JMenu mHelp = new JMenu("帮助");
JMenuItem mAbout = new JMenuItem("关于");
JMenuBar mBar = new JMenuBar();
MainForm() {
super("学生成绩管理系统");
setSize(700, 630);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mSystem.add(mExit);
mOperate.add(mAdd);
mOperate.add(mDel);
mOperate.add(mModify);
mOperate.add(mBrowse);
mQuery.add(mNumber);
mQuery.add(mScore);
mHelp.add(mAbout);
mBar.add(mSystem);
mBar.add(mOperate);
mBar.add(mQuery);
mBar.add(mHelp);
setJMenuBar(mBar);
mExit.addActionListener(this);
mAdd.addActionListener(this);
mDel.addActionListener(this);
mModify.addActionListener(this);
mBrowse.addActionListener(this);
mNumber.addActionListener(this);
mScore.addActionListener(this);
mAbout.addActionListener(this);
setContentPane(new MyPanel());
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == mExit)
System.exit(0);
else if (ae.getSource() == mAbout)
JOptionPane.showMessageDialog(this,
"学生管理系统 V1.0\n\n重庆邮电大学计算机学院\n\n2011年11月", "关于",
JOptionPane.INFORMATION_MESSAGE);
else if (ae.getSource() == mAdd)
new AddForm().setVisible(true);
else if (ae.getSource() == mDel)
new DeleteForm().setVisible(true);
else if (ae.getSource() == mModify)
new ModifyForm().setVisible(true);
else if (ae.getSource() == mBrowse)
new BrowseForm().setVisible(true);
else if (ae.getSource() == mNumber)
new NumberQueryForm().setVisible(true);
else if (ae.getSource() == mScore)
new ScoreQueryForm().setVisible(true);
}
public static void main(String[] args) {
new MainForm();
}
}
[源代码打包下载]
初级程序员
by: 云代码会员 发表于:2013-08-19 14:09:35 顶(3) | 踩(2) 回复
怎么没有图?
回复评论