📄 addteacherinfo.java
字号:
package manager;
import java.awt.Dimension;
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.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class AddTeacherInfo extends JFrame implements ActionListener {
public JTextField textField0;
public JTextField textField1;
public JTextField textField2;
public JTextField textField3;
private JButton button0;
private JButton button1;
private JButton button2;
/**
*
*/
private static final long serialVersionUID = 1L;
public AddTeacherInfo() {
super();
getContentPane().setLayout(null);
final JLabel label0 = new JLabel();
label0.setFont(new Font("宋体", Font.PLAIN, 14));
label0.setText("教师姓名:");
label0.setBounds(57, 123, 81, 28);
getContentPane().add(label0);
textField1 = new JTextField();
textField1.setFont(new Font("宋体", Font.PLAIN, 12));
textField1.setBounds(126, 116, 89, 24);
getContentPane().add(textField1);
textField2 = new JTextField();
textField2.setFont(new Font("宋体", Font.PLAIN, 12));
textField2.setBounds(126, 172, 89, 24);
getContentPane().add(textField2);
final JLabel label1 = new JLabel();
label1.setFont(new Font("宋体", Font.PLAIN, 14));
label1.setText("教师电话:");
label1.setBounds(57, 169, 75, 29);
getContentPane().add(label1);
final JLabel label0_1 = new JLabel();
label0_1.setFont(new Font("宋体", Font.PLAIN, 14));
label0_1.setText("用户名:");
label0_1.setBounds(57, 32, 81, 28);
getContentPane().add(label0_1);
textField3 = new JTextField();
textField3.setEditable(false);
textField3.setFont(new Font("宋体", Font.PLAIN, 12));
textField3.setBounds(126, 35, 89, 24);
getContentPane().add(textField3);
textField0 = new JTextField();
textField0.setEditable(false);
textField0.setFont(new Font("宋体", Font.PLAIN, 12));
textField0.setBounds(126, 81, 89, 24);
getContentPane().add(textField0);
final JLabel label1_1 = new JLabel();
label1_1.setFont(new Font("宋体", Font.PLAIN, 14));
label1_1.setText("密码:");
label1_1.setBounds(57, 78, 75, 29);
getContentPane().add(label1_1);
button0 = new JButton();
button0.setFont(new Font("宋体", Font.PLAIN, 12));
button0.setText("添加");
button0.addActionListener(this);
button0.setBounds(10, 234, 70, 27);
getContentPane().add(button0);
button1 = new JButton();
button1.setFont(new Font("宋体", Font.PLAIN, 12));
button1.setText("重填");
button1.addActionListener(this);
button1.setBounds(115, 234, 70, 27);
getContentPane().add(button1);
button2 = new JButton();
button2.setFont(new Font("宋体", Font.PLAIN, 12));
button2.setText("取消");
button2.addActionListener(this);
button2.setBounds(221, 234, 60, 27);
getContentPane().add(button2);
}
public void ShowFrame() {
Dimension screen = getToolkit().getScreenSize(); // 封装组件,工具包中设置屏幕大小的方法
int width = (int) ((screen.getWidth() - 300) / 2);
int height = (int) ((screen.getHeight() - 320) / 2);// 设置对象宽 ,高
this.setBounds(width, height, 300, 320);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == button0) {// 添加
String id;
String name;
String pwd;
String tel;
int i = 0;
id = this.textField3.getText();
name = this.textField1.getText();
pwd = this.textField0.getText();
tel = this.textField2.getText();
Statement statement0 = null;
Connection conn = null;
try {
try {
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
} catch (InstantiationException e0) {
// TODO Auto-generated catch block
e0.printStackTrace();
} catch (IllegalAccessException e0) {
// TODO Auto-generated catch block
e0.printStackTrace();
}
String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=KeyManager";
String user = "sa";
String password = "sa";
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e0) {
e0.getStackTrace();
// conn = getJOConnection();
} catch (ClassNotFoundException e0) {
e0.getStackTrace();
// conn = getJOConnection();
}
String statement = "Insert into TeacherInfo(TeacherId,TeacherName,TeacherPwd,TeacherTel) values ('"
+ id + "','" + name + "','" + pwd + "','" + tel + "')";
System.out.println(statement);
try {
statement0 = conn.createStatement();
i = statement0.executeUpdate(statement);
} catch (SQLException e0) {
// TODO 自动生成 catch 块
e0.printStackTrace();
}
if (i > 0) {
JOptionPane.showMessageDialog(this, "添加成功");
this.setVisible(false);
} else {
JOptionPane.showMessageDialog(this, "添加失败,请检查一下格式,重新添加");
this.textField1.setText("");
this.textField2.setText("");
}
} else if (e.getSource() == button1)// 重填
{
this.textField1.setText("");
this.textField2.setText("");
} else if (e.getSource() == button2)// 取消
{
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -