package com.company;
import java.awt.*;//用于创建用户界面和绘制图像的所有类
import java.awt.event.*;//用于处理AWT组件触发的不同类型事件的接口和类
import javax.swing.*;//提供“轻量级”组件,尽量让组件在所有平台工作方式相同
import java.sql.*;//进行服务器端数据源访问和处理
/**
* Qframe(基本控件控制)
*/
class Qframe extends JFrame implements ActionListener{//ActionListener响应继承图形化界面的基本控件
JPanel contentPane;//定义JPanel类型的容器
BorderLayout borderLayout1=new BorderLayout(5,10);// 构造一个具有指定组件(横向间距,纵向间距)间距的边框布局
Label prompt;//显示提示对话框
JTextField stm;//定义一个JTextField文本框
Button run;//定义Button按钮
public static final TextArea result=new TextArea();
//构造基本控件方法,初始化组件
public Qframe(){
contentPane=(JPanel)this.getContentPane();//成员变量
contentPane.setLayout(borderLayout1);
this.setTitle("教师信息系统");
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
prompt =new Label("执行语句:");
run=new Button("查询");
result.setEditable(false);
stm=new JTextField(100);
JPanel option =new JPanel();
Button zenjia=new Button("增加(add)");
option.add(zenjia,BorderLayout.NORTH);
zenjia.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==zenjia){
AddDialog add1= new AddDialog();
add1.setVisible(true);
}
}
});
zenjia.addActionListener(this);
Button shanchu=new Button("删除(delet)");
option.add(shanchu,BorderLayout.NORTH);
shanchu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==shanchu){
DeleteDialog dt1=new DeleteDialog();
dt1.setVisible(true);
}
}
});
shanchu.addActionListener(this);
Button xiugai=new Button("修改(alter)");
option.add(xiugai,BorderLayout.NORTH);
xiugai.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==xiugai){
AlterDialog af=new AlterDialog();
af.setVisible(true);//设置可见
}
}
});
//查询
Button cx=new Button("查询(query)");
option.add(cx,BorderLayout.NORTH);
cx.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==cx){
CxDialog cj=new CxDialog();
cj.setVisible(true);
cj.show();
}
}
});
cx.addActionListener(this);
xiugai.addActionListener(this);
Button chaxun=new Button("刷新(refresh)");
option.add(chaxun,BorderLayout.NORTH);
chaxun.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==chaxun){
Connection sin=Dbcon.getconnectin();
ResultSet rs=Dbcon.query(sin);
try {
result.setText("刷新成功\n\n\n");
while(rs.next())
{
String msg=rs.getString("工号")+"\t"+rs.getString("姓名")+"\t"+rs.getString("性别")+"\t"+rs.getString("年龄")+"\t"+rs.getString("职称")+"\t"+rs.getString("教研室")+"\t"+rs.getString("授课名称")+"\t"+rs.getString("入职时间")+"\n";
javax.swing.JOptionPane.showMessageDialog(null,"result.append(msg)");
}
sin.close();
rs.close();
}catch(Exception h){
h.printStackTrace();
}
}
}
});
chaxun.addActionListener(this);
Button tuichu=new Button("退出(quit)");
option.add(tuichu,BorderLayout.NORTH);
tuichu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tuichu){
System.exit(0);
}
}
});
//界面布局
contentPane.add(prompt,BorderLayout.WEST);
contentPane.add(stm,BorderLayout.CENTER);
contentPane.add(run,BorderLayout.EAST);
contentPane.add(result,BorderLayout.SOUTH);
contentPane.add(option,BorderLayout.NORTH);
run.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()==run){
String url="jdbc:sqlserver://localhost:1433;DatabaseName=ZNPB";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//System.out.println("数据库驱动加载成功");
String user="sa";
String password="123456";
Connection con=DriverManager.getConnection(url,user,password);
System.out.println("连接成功");
con=Dbcon.getconnectin();
String msg1="connected successfully!";
result.setText("连接成功\n\n\n");
String yuju=stm.getText();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(yuju);
while(rs.next())
{
String msg=rs.getString("TNo")+"\t"+rs.getString("TN")+"\t"+rs.getString("TSex")+"\t"+rs.getString("TAge")+"\t"+rs.getString("TPro")+"\t"+rs.getString("TDept")+"\t"+rs.getString("TCourse")+"\t"+rs.getString("TEtime")+"\n";
result.append(msg);
}
rs.close();
st.close();
con.close();
//System.out.println("连接完成");
}catch(SQLException sqle){
result.setText(sqle+"connected failed!");
//System.out.println(sqle+"connected failed!");
}
catch(Exception h){
result.setText(h+"connection error!");
h.printStackTrace();
//System.out.println(h+"connecttion error!");
}
}
}
//@SuppressWarnings("deprecation")
public static void main(String argc[]){
LoginIn dl=new LoginIn();
dl.show();
Qframe frame= new Qframe();
frame.setLocation(400,200);
frame.resize(400,285);
frame.show();
}
}