📄 kechengguanli.java
字号:
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class kechengguanli extends JFrame implements ActionListener
{
Connection con;
ResultSet rs;
Statement st;
JButton button1,button2,button3,button4;
JTextField text1,text2,text3,text4;
String crid1=new String();
String crname1=new String();
String crper1=new String();
String crcre1=new String();
public void kechengguanli()
{
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
text4=new JTextField(10);
button1=new JButton("查看");
button1.addActionListener(this);
button2=new JButton("添加");
button2.addActionListener(this);
button3=new JButton("删除");
button3.addActionListener(this);
button4=new JButton("退出");
button4.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("课程管理页面",JLabel.CENTER));
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("课名:",JLabel.CENTER));
box2.add(text1);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("课号:",JLabel.CENTER));
box3.add(text2);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("学时:",JLabel.CENTER));
box4.add(text3);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("学分:",JLabel.CENTER));
box5.add(text4);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(button1);
pSouth.add(button2);
pSouth.add(button3);
pSouth.add(button4);
add(pSouth,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,50,400,300);
validate();
}
public void actionPerformed(ActionEvent e)
{ try{//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e2)
{
System.out.println(""+e2);
}
try{
con=DriverManager.getConnection("jdbc:odbc:lhm","","");
st=con.createStatement();
rs=st.executeQuery("Select * FROM kecheng");
}
catch(SQLException el){}//连接数据库
if(e.getSource()==button1)
{
String crname=new String();//提取课程名
crname=text1.getText();
int count=0;
try{
while(rs.next())
{crname1=rs.getString("crname");
if(crname1.equals(crname))
{crid1=rs.getString("crid");
crper1=rs.getString("crper");
crcre1=rs.getString("crcre");
text2.setText(crid1);
text3.setText(crper1);
text4.setText(crcre1);
++count;
}
}
}catch(Exception ee){}
if(count==0){JOptionPane.showMessageDialog(null,"该学期未开这么课程!");text1.setText("");}
}
if(e.getSource()==button2)
{
String crid2=new String();
crid2=text2.getText();
String crname2=new String();
crname2=text1.getText();
String crper2=new String();
crper2=text3.getText();
String crcre2=new String();
crcre2=text4.getText();
if((crid2.length()>0)&&(crname2.length()>0)&&(crper2.length()>0)&&(crcre2.length()>0))
{int count=0;
try{
while(rs.next())
{ String crid3=new String();
crid3=rs.getString("crid");
if(crid2.equals(crid3)){count++;}
}
}catch(Exception exxx){}
if(count==0)
{
String strSQL6 ="insert into kecheng(crid,crname,crper,crcre) values('" + crid2 + "','" + crname2 + "','" + crper2+"','" + crcre2+"')";
try{
st.executeUpdate(strSQL6);
}catch(Exception exx){}
JOptionPane.showMessageDialog(null,"添加成功!");
}
else{JOptionPane.showMessageDialog(null,"课程号已存在,请重新填写!");}
}
else{JOptionPane.showMessageDialog(null,"请完整填写课程信息");}
}
if(e.getSource()==button3)
{
String m="确定要删除该门课程吗?";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{String crname2=new String();
crname2=text1.getText();
String strSQL5 ="delete from kecheng where crname='"+crname2+"'";
try
{
st.executeUpdate(strSQL5);
}catch(Exception exxxx){}
JOptionPane.showMessageDialog(null,"删除成功!");
}
}
if(e.getSource()==button4){setVisible(false);}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -