学生信息管理,给分吧。。。。
//
//
package com.lz.db;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableColumn;
public class Student extends JFrame implements ActionListener{
JDesktopPane desktop;
public Student(){
super("学生");
int inset = 50;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height - inset*2);
//Set up the GUI.
desktop = new JDesktopPane(); //a specialized layered pane
setContentPane(desktop);
setJMenuBar(createMenuBar());
//Make dragging a little faster but perhaps uglier.
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}
//
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("数据库操作");
menu.setMnemonic(KeyEvent.VK_D);
menuBar.add(menu);
JMenuItem menuItem;
//
menuItem = new JMenuItem("浏览");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("select");
menuItem.addActionListener(this);
menu.add(menuItem);
//
menuItem = new JMenuItem("增加");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("insert");
menuItem.addActionListener(this);
menu.add(menuItem);
//
menuItem = new JMenuItem("修改");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("update");
menuItem.addActionListener(this);
menu.add(menuItem);
//
menuItem = new JMenuItem("删除");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("delete");
menuItem.addActionListener(this);
menu.add(menuItem);
//Set up the second menu item.
menuItem = new JMenuItem("退出");
menuItem.setMnemonic(KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
menuItem.setActionCommand("quit");
menuItem.addActionListener(this);
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
//React to menu selections.
public void actionPerformed(ActionEvent e) {
if ("new".equals(e.getActionCommand())) { //new
createFrame();
}
else if("select".equals(e.getActionCommand())){
createBrowFrame();
}
else if("insert".equals(e.getActionCommand())){
createInsertFrame();
}
else if("delete".equals(e.getActionCommand())){
createDeleteFrame();
}
else if("update".equals(e.getActionCommand())){
createUpdateFrame();
}
else { //quit
quit();
}
}
//
protected void createUpdateFrame(){
UpdateFrame frame=new UpdateFrame();
frame.setVisible(true);
desktop.add(frame);
try{
frame.setSelected(true);
}catch(Exception e){
}
}
//
protected void createDeleteFrame(){
DeleteFrame frame=new DeleteFrame();
frame.setVisible(true);
desktop.add(frame);
try{
frame.setSelected(true);
}catch(Exception e){
}
}
//
protected void createInsertFrame(){
InsertFrame frame=new InsertFrame();
frame.setVisible(true);
desktop.add(frame);
try{
frame.setSelected(true);
}catch(Exception e){
}
}
protected void createBrowFrame(){
BrowFrame frame=new BrowFrame();
frame.setVisible(true);
desktop.add(frame);
try{
frame.setSelected(true);
}catch(Exception e){
}
}
//Create a new internal frame.
protected void createFrame() {
BrowFrame frame = new BrowFrame();
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
//Quit the application.
protected void quit() {
System.exit(0);
}
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
Student frame = new Student();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class BrowFrame extends JInternalFrame implements ActionListener{
private JLabel label1,label2,label3;
private JTextField snamefield,snofield,sbirthdayfield;
private JButton okButton;
private JPanel p2;
private DbHelper helper;
//public
public BrowFrame(){
super("BROW" ,
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
this.setTitle("brow");
setSize(700,300);
String sql;
sql="select * from tblstudent";
helper=new DbHelper();
ResultSet rs=helper.getRs(sql);
ResultSetMetaData md;
int columns=0;
Vector columnNames=new Vector();
Vector data=new Vector();
try{
md=rs.getMetaData();
columns=md.getColumnCount();
int i;
for(i=1;i<=columns;i++){
columnNames.addElement(md.getColumnName(i));
}
while(rs.next()){
Vector row=new Vector(columns);
for(i=1;i<=columns;i++){
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
}catch(Exception e){
}
JTable table=new JTable(data,columnNames);
TableColumn col;
int i;
for(i=0;i<table.getColumnCount();i++){
col=table.getColumnModel().getColumn(i);
col.setMaxWidth(250);
}
JScrollPane scrollPane=new JScrollPane(table);
p2=new JPanel();
p2.add(scrollPane);
this.getContentPane().add(p2,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e){
}
}
class DeleteFrame extends JInternalFrame implements ActionListener{
private JLabel label1,label2,label3;
private JTextField snamefield,snofield,sbirthdayfield;
private JButton okButton;
private JPanel p2;
private DbHelper helper;
public DeleteFrame(){
super("删除" ,
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
this.setTitle("delete");
setSize(700,300);
label1=new JLabel("name");
snamefield=new JTextField(10);
label2=new JLabel("sno");
snofield=new JTextField(10);
label3=new JLabel("出生年月");
sbirthdayfield=new JTextField(10);
okButton=new JButton("确定");
okButton.setActionCommand("ok");
okButton.addActionListener(this);
p2=new JPanel();
p2.add(label1);
p2.add(snamefield);
p2.add(label2);
p2.add(snofield);
//p2.add(label3);
//p2.add(sbirthdayfield);
p2.add(okButton);
this.getContentPane().add(p2,BorderLayout.NORTH);
helper=new DbHelper();
}
public void actionPerformed(ActionEvent e){
String sql=null;
if(snamefield.getText().trim().length()!=0 && snofield.getText().trim().length()!=0 ){
sql="delete from tblstudent where sname='"+snamefield.getText().trim()+"' and sno='"
+snofield.getText().trim()+"'";
}
else if(snamefield.getText().trim().length()!=0 ){
sql="delete from tblstudent where sname='"+snamefield.getText().trim()+"'";
}
else if(snofield.getText().trim().length()!=0){
sql="delete from tblstudent where sno='"+snofield.getText().trim()+"'";
}
System.out.println(sql);
if(helper.insert(sql)){
JOptionPane.showMessageDialog(null, "delete success");
}
}
}
class InsertFrame extends JInternalFrame implements ActionListener{
private JLabel label1,label2,label3;
private JTextField snamefield,snofield,sbirthdayfield;
private JButton okButton;
private JPanel p2;
private DbHelper helper;
//public
public InsertFrame(){
super("增加" ,
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
this.setTitle("insert");
setSize(700,300);
label1=new JLabel("姓名");
snamefield=new JTextField(10);
label2=new JLabel("学号");
snofield=new JTextField(10);
label3=new JLabel("出生年月");
sbirthdayfield=new JTextField(10);
okButton=new JButton("确定");
okButton.setActionCommand("ok");
okButton.addActionListener(this);
p2=new JPanel();
p2.add(label1);
p2.add(snamefield);
p2.add(label2);
p2.add(snofield);
p2.add(label3);
p2.add(sbirthdayfield);
p2.add(okButton);
this.getContentPane().add(p2,BorderLayout.NORTH);
helper=new DbHelper();
}
public void actionPerformed(ActionEvent e){
String sql;
sql="insert into tblstudent([sname],[sno],[sbirthday])values('"+snamefield.getText()+"','"+snofield.getText()
+"','"+sbirthdayfield.getText()+"')";
System.out.println(sql);
if(helper.insert(sql)){
JOptionPane.showMessageDialog(null, "增加成功");
}
}
}
class DbHelper{
private static String filename="E:\\will\\prj\\tmp\\demo.mdb";
private static Connection conn=null;
private static String database="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
private Statement s;
public DbHelper(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(database+filename,"","");
s =conn.createStatement();
}catch(Exception e){
System.out.println("Error: "+e);
}
}
public Boolean insert(String sql){
Boolean b=false;
try{
s.executeUpdate(sql);
b=true;
}catch(Exception e){
System.out.println("Error: "+e);
}
return b;
}
public ResultSet getRs(String sql){
ResultSet rs =null;
try{
rs=s.executeQuery(sql);
}catch(Exception e){
System.out.println(e);
}
return rs;
}
}
class UpdateFrame extends JInternalFrame implements ActionListener{
private JTextField snamefield,snofield,sbirthdayfield,oldsnamefield;
private JLabel label1,label2,label3,label4;
private JButton okButton;
public UpdateFrame(){
super("update" ,
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
this.setTitle("update");
setSize(700,300);
label1=new JLabel("oldname");
oldsnamefield=new JTextField(10);
label2=new JLabel("new name");
snamefield=new JTextField(10);
label3=new JLabel("new no");
snofield=new JTextField(10);
label4=new JLabel("new birthday");
sbirthdayfield=new JTextField(10);
okButton=new JButton("确定");
okButton.setActionCommand("ok");
okButton.addActionListener(this);
p2=new JPanel();
p2.add(label1);
p2.add(oldsnamefield);
p2.add(label2);
p2.add(snamefield);
p2.add(label3);
p2.add(snofield);
p2.add(label4);
p2.add(sbirthdayfield);
p2.add(okButton);
this.getContentPane().add(p2,BorderLayout.CENTER);
helper=new DbHelper();
}
private DbHelper helper;
public void actionPerformed(ActionEvent e){
String sql;
sql="update tblstudent set sname='"+snamefield.getText().trim()+"',sno='"+snofield.getText().trim()
+"',sbirthday='"+sbirthdayfield.getText().trim()+"' where sname='"+oldsnamefield.getText().trim()+"'";
System.out.println(sql);
if(helper.insert(sql)){
JOptionPane.showMessageDialog(null, "update success");
}
}
private JPanel p2,p3;
}
温馨提示:内容为网友见解,仅供参考