代码拉取完成,页面将自动刷新
package demo;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
//=========教师登录界面======//
//======教师信息表就相当于课表=====//
//====管理员修改课表就是修改教师信息表=======//
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
class ShowTeacher extends JFrame{
//定义组件
JTable jt;
JScrollPane jsp;
JPanel pCenter=new JPanel();
JPanel pSouth=new JPanel();
JButton b1=new JButton("返回");
JButton b2=new JButton("退出");
//定义JTable对象
Vector rowData ,columnNames;
//定义一行数据
Vector<String> line1;
//定义数据库对象
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
public ShowTeacher(String num) throws SQLException{
//设置表格类目
columnNames=new Vector();
columnNames.add("教师号");
columnNames.add("姓名");
columnNames.add("年龄");
columnNames.add("教授课程");
columnNames.add("教授地点");
columnNames.add("教授班级");
columnNames.add("开始时间");
columnNames.add("结束时间");
//设置表格数据
rowData=new Vector();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=new", "sa", "123456");
pstmt=conn.prepareStatement("select * from teacher where 教师号 = ?");
pstmt.setString(1, num);
rs=pstmt.executeQuery();
while(rs.next()) {
line1=new Vector<String>();
line1.add(rs.getString(1));
line1.add(rs.getString(2));
line1.add(rs.getString(3));
line1.add(rs.getString(4));
line1.add(rs.getString(5));
line1.add(rs.getString(6));
line1.add(rs.getString(7));
line1.add(rs.getString(8));
rowData.add(line1);
}
JFrame fr=this;
jt=new JTable(rowData,columnNames);
jsp=new JScrollPane(jt);
this.add(jsp);
this.setTitle("教师信息");
this.setLocation(200,300);
this.setSize(1400,600);
jt.setFont(new Font("宋体",Font.BOLD,15));
jt.setRowHeight(120);
pCenter.setLayout(new BorderLayout());
pSouth.setLayout(new FlowLayout());
this.add(pCenter,BorderLayout.CENTER);
this.add(pSouth,BorderLayout.SOUTH);
pCenter.add(jsp);
pSouth.add(b1);
pSouth.add(b2);
//返回
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
fr.dispose();
new Student();
}
});
//退出
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "系统被关闭","警告", 2);
System.exit(1);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
public class Teacher {
//准备组件
JFrame frame=new JFrame("教师登录");
JLabel l1=new JLabel("用户名:");
JLabel l2=new JLabel("密码:");
JTextField t1=new JTextField(10);
JPasswordField t2=new JPasswordField(10);
JButton lg=new JButton("登录");
JButton back=new JButton("返回");
JButton exit=new JButton("退出");
Connection conn;
public Teacher() {
//设置登录界面
frame.setSize(400,300);
frame.setLocation(500, 200);
frame.setLayout(null);
l1.setBounds(40, 20, 80, 40);
frame.add(l1);
t1.setBounds(125,20, 180, 40);
frame.add(t1);
l2.setBounds(60,80,80,40);
frame.add(l2);
t2.setBounds(125,80,180,40);
frame.add(t2);
lg.setBounds(80,140,80,40);
frame.add(lg);
back.setBounds(200,140,80,40);
frame.add(back);
exit.setBounds(80,190,200,40);
frame.add(exit);
//============连接数据库
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("数据库驱动加载成功");
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=new", "sa", "123456");
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("数据库连接成功");
//=============登录功能
lg.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent arg0) {
String name=t1.getText();//获得账号
String password=t2.getText();//获得密码
System.out.println(name);
System.out.println(password);
PreparedStatement pstmt;
ResultSet rs;
String sql="select * from teacher where 教师号 = ?";
try {
pstmt=conn.prepareStatement(sql);
if(name.equals(""))
{
JOptionPane.showMessageDialog(null, "账号为空!!");
t1.setText("");
t2.setText("");
/* frame.dispose();
new Student();*/
}
else if(password.equals("")){
JOptionPane.showMessageDialog(null, "密码为空!!");
t1.setText("");
t2.setText("");
/*frame.dispose();
new Student();*/
}
pstmt.setString(1, name);
rs=pstmt.executeQuery();
if(rs.next()&&!password.equals("")) {
if(rs.getString(9).equals(password)) {
JOptionPane.showMessageDialog(null, "登录成功!!");
t1.setText("");
t2.setText("");
frame.dispose();;
new ShowTeacher(name);
}
else {
JOptionPane.showMessageDialog(null, "密码输入错误!!\n请重新输入!!","提示",2);
t1.setText("");
t2.setText("");
}
}
else {
JOptionPane.showMessageDialog(null, "账号输入错误!!\n请重新输入!!","提示",2);
t1.setText("");
t2.setText("");
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
//返回功能
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
new Login();
}
});
//退出功能
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "系统被关闭","警告", 2);
System.exit(1);
}
});
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
public static void main(String[] args) {
new Teacher();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。