?? courseedit.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
import javax.swing.table.*;
class CourseEdit extends JFrame implements ActionListener
{
JFrame f=new JFrame("課程設置");
JLabel lbcourse=new JLabel("課程"),
lbclass=new JLabel("班級"),
lbterm=new JLabel("學期"),
lbtishi=new JLabel("請在輸入班級后按回車");
JComboBox cbxterm=new JComboBox();
JTextField tfclass=new JTextField(10),
tfcourse=new JTextField(10);
JPanel p1=new JPanel(),
p2=new JPanel(),
p3=new JPanel(),
p4=new JPanel(),
p5=new JPanel();
JScrollPane sp=new JScrollPane();
JButton btnadd=new JButton("添加 "),
btndelete=new JButton("刪除 "),
btnsearch=new JButton("查詢"),
btnexit=new JButton("退出 ");
Vector tempvector1;
DefaultTableModel model=new DefaultTableModel();
JTable TableResult=new JTable(model);
public CourseEdit()
{
Container cp=f.getContentPane();
cp.setLayout(new BorderLayout());
p5.setLayout(new GridLayout(4,1));
p1.add(lbclass);p1.add(tfclass);p1.add(lbtishi);
p2.add(lbterm);p2.add(cbxterm);p2.add(btnsearch);
p3.add(lbcourse);p3.add(tfcourse);
p4.add(btnadd);p4.add(btndelete);p4.add(btnexit);
p5.add(p1);p5.add(p2);p5.add(p3);p5.add(p4);
cp.add("North",p5);cp.add("Center",sp);
model.addColumn("班級") ;
model.addColumn("學期") ;
model.addColumn("課程") ;
sp.getViewport().add(TableResult,null);
btnadd.addActionListener(this);
btndelete.addActionListener(this);
btnexit.addActionListener(this);
tfclass.addActionListener(this);
f.setSize(400,400);
f.setVisible(true);
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tfclass)
{
/////初始化選擇框Combobox組件
try{
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
String strSQL="select term from mcterm where class='"+tfclass.getText().trim()+"'";
ResultSet rs=s.executeQuery(strSQL);
cbxterm.removeAllItems();
while(rs.next())
{
cbxterm.addItem(rs.getString("term"));
}
}
catch(Exception ee)
{System.out.println("輸入班級點回車錯誤 "+ee.getMessage()); }
/////初始化選擇框Combobox組件
}
/*
if(e.getSource()==btnserch)
{
if(tfmajor.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(this,"抱歉,您還沒有輸入查詢的內容,請輸入查詢的內容后再查詢 ");
tfmajor.setText(null);
}
else{
for(int i=model.getRowCount()-1;i>=0;i--)
{ model.removeRow(i); }
try{
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
String strSQL="select * from mcterm where major='"+tfmajor.getText().trim()+"'";
ResultSet rs=s.executeQuery(strSQL);
if(!rs.next())
{ JOptionPane.showMessageDialog(this,"抱歉,還沒有您要查詢的記錄,或者您的輸入不符合查詢的條件,請確認后重新輸入 ");
tfmajor.setText(null);
}
else{
do {
tempvector1=new Vector(1,1);
tempvector1.add(rs.getString("major"));
tempvector1.add(rs.getString("class"));
tempvector1.add(rs.getString("term"));
model.addRow(tempvector1);
}while(rs.next());
}
s.close();
con.close();
}
catch(Exception ee)
{System.out.println("信息查詢出錯: "+ee.getMessage()); }
}
}
if (e.getSource()==btnclear)
{
tfmajor.setText(null);
tfclass.setText(null);
tfterm.setText(null);
for(int i=model.getRowCount()-1;i>=0;i--)
{ model.removeRow(i); }
}
if (e.getSource()==btnadd)
{
try{
if((tfmajor.getText().equals(""))||(tfclass.getText().equals("")) ||(tfterm.getText().equals("")) )
{JOptionPane.showMessageDialog(null,"抱歉!您還有相關的信息沒有輸入。請輸入相應的信息再點擊添加按鈕!"); }
else
{String strSQL="insert into mcterm(major,class,term) values('"+tfmajor.getText()+"','"+tfclass.getText()+"','"+tfterm.getText()+"')";
String strall="select * from mcterm";
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
s.executeUpdate(strSQL);
JOptionPane.showMessageDialog(null,"恭喜你,添加成功! ");
tfmajor.setText(null);
tfclass.setText(null);
tfterm.setText(null);
ResultSet rs=s.executeQuery(strall);
for(int i=model.getRowCount()-1;i>=0;i--)
{ model.removeRow(i); }
while(rs.next())
{ tempvector1=new Vector(1,1);
tempvector1.add(rs.getString("major"));
tempvector1.add(rs.getString("class"));
tempvector1.add(rs.getString("term"));
model.addRow(tempvector1);
}
}
}
catch(Exception ee)
{System.out.println("添加信息出錯:"+ee.getMessage()); }
}/*
if (e.getSource()==btndelete)
{
if(tfmajor.getText().trim().equals(""))
{ JOptionPane.showMessageDialog(this,"抱歉,專業不可以為空,無法刪除");}
else{
try{
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
String id = tfsid.getText();
String strSQL = "delete * from mcterm where major= '"+tfmajor.getText().trim()+"' ";
s.executeUpdate(strSQL);
}
catch(Exception exx)
{JOptionPane.showMessageDialog(null,"抱歉,出錯了");}
JOptionPane.showMessageDialog(null,"刪除成功!");
tfmajor.setText(null);
tfphone.setText(null);
tfjiguan.setText(null);
}
}*/
if (e.getSource()==btnexit) { f.setVisible(false);}
}
public static void main(String args[])
{
String JDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String conURL="jdbc:odbc:Xsstudent";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println("加載驅動失敗");}
new CourseEdit();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -