📄 teacherinfo.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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import domin.Teacher;
public class TeacherInfo extends JInternalFrame implements ActionListener {
public JTextField textField1;
public JTextField textField;
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private static ArrayList arraylist;// 存储教师信息
private static Teacher teacher;
static int Index;
/**
* 教师信息编辑
*/
private static final long serialVersionUID = 1L;
public TeacherInfo() {
super("修改密码",true,true,true);
arraylist = new ArrayList();
Index = 1;
getContentPane().setLayout(null);
final JLabel label0 = new JLabel();
label0.setFont(new Font("宋体", Font.PLAIN, 14));
label0.setText("教师姓名:");
label0.setBounds(71, 45, 81, 28);
getContentPane().add(label0);
final JLabel label1 = new JLabel();
label1.setFont(new Font("宋体", Font.PLAIN, 14));
label1.setText("教师电话:");
label1.setBounds(71, 91, 75, 29);
getContentPane().add(label1);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.PLAIN, 12));
textField.setBounds(140, 48, 89, 24);
getContentPane().add(textField);
textField1 = new JTextField();
textField1.setFont(new Font("宋体", Font.PLAIN, 12));
textField1.setBounds(140, 94, 89, 24);
getContentPane().add(textField1);
button0 = new JButton();
button0.setFont(new Font("宋体", Font.PLAIN, 12));
button0.setText("修改");
button0.addActionListener(this);
button0.setBounds(235, 93, 72, 27);
getContentPane().add(button0);
button1 = new JButton();
button1.setFont(new Font("宋体", Font.PLAIN, 12));
button1.setText("删除");
button1.addActionListener(this);
button1.setBounds(313, 93, 71, 27);
getContentPane().add(button1);
button2 = new JButton();
button2.setFont(new Font("宋体", Font.PLAIN, 12));
button2.setText("首记录");
button2.addActionListener(this);
button2.setBounds(15, 184, 90, 27);
getContentPane().add(button2);
button3 = new JButton();
button3.setFont(new Font("宋体", Font.PLAIN, 12));
button3.setText("上一条记录");
button3.addActionListener(this);
button3.setBounds(111, 184, 95, 27);
getContentPane().add(button3);
button4 = new JButton();
button4.setFont(new Font("宋体", Font.PLAIN, 12));
button4.setText("下一条记录");
button4.addActionListener(this);
button4.setBounds(212, 184, 95, 27);
getContentPane().add(button4);
button5 = new JButton();
button5.setFont(new Font("宋体", Font.PLAIN, 12));
button5.setText("尾记录");
button5.addActionListener(this);
button5.setBounds(313, 184, 90, 27);
getContentPane().add(button5);
button6 = new JButton();
button6.setFont(new Font("宋体", Font.PLAIN, 12));
button6.setText("添加新记录");
button6.addActionListener(this);
button6.setBounds(255, 46, 100, 27);
getContentPane().add(button6);
}
public void ShowFrame() {
Dimension screen = getToolkit().getScreenSize(); // 封装组件,工具包中设置屏幕大小的方法
int width = (int) ((screen.getWidth() - 425) / 2);
int height = (int) ((screen.getHeight() - 280) / 2);// 设置对象宽 ,高
this.setBounds(width, height, 425, 280);
this.setVisible(true);
}
public ArrayList getArraylist() {
return arraylist;
}
public void setArraylist(ArrayList arraylist) {
TeacherInfo.arraylist = arraylist;
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource() == button0)// 修改
{
}
if (arg0.getSource() == button1)// 删除
{
int i = 0;
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 = "Delete from TeacherInfo where TeacherName ='"
+ textField.getText() + "'and TeacherTel='"
+ textField1.getText() + "'";
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, "删除失败");
}
}
if (arg0.getSource() == button2)// 首记录
{
Index = 0;
System.out.println("首记录" + arraylist.size());
teacher = (Teacher) arraylist.get(Index);
System.out.println(teacher.getTeacherName());
this.textField.setText(teacher.getTeacherName());
this.textField1.setText(teacher.getTeacherTel());
}
if (arg0.getSource() == button3)// 上记录
{
if (Index == 0) {
JOptionPane.showMessageDialog(this, "这是第一条记录");
} else {
Index = Index - 1;
Teacher teacher = new Teacher();
teacher = (Teacher) arraylist.get(Index);
teacher = (Teacher) arraylist.get(Index);
this.textField.setText(teacher.getTeacherName());
this.textField1.setText(teacher.getTeacherTel());
}
}
if (arg0.getSource() == button4)// 下记录
{
if (Index == arraylist.size() - 1) {
JOptionPane.showMessageDialog(this, "这是最后一条记录");
} else {
Index = Index + 1;
teacher = (Teacher) arraylist.get(Index);
this.textField.setText(teacher.getTeacherName());
this.textField1.setText(teacher.getTeacherTel());
}
}
if (arg0.getSource() == button5)// 尾记录
{
Index = arraylist.size() - 1;
teacher = (Teacher) arraylist.get(Index);
System.out.println(teacher.getTeacherName());
this.textField.setText(teacher.getTeacherName());
this.textField1.setText(teacher.getTeacherTel());
}
if (arg0.getSource() == button6)// 添加新记录
{
int NewIndex = 0;
ResultSet rs = null;
Connection conn = null;
try {
try {
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=KeyManager";
String user = "sa";
String password = "sa";
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.getStackTrace();
// conn = getJOConnection();
} catch (ClassNotFoundException e) {
e.getStackTrace();
// conn = getJOConnection();
}
String statement = "select TeacherId as NewIndex from TeacherInfo";
System.out.println(statement);
try {
rs = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE).executeQuery(statement);
while (rs.next()) {
NewIndex = Integer.parseInt(rs.getString("NewIndex"));
System.out.println("新教师用户名" + NewIndex);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
NewIndex = NewIndex + 1;// 信用户的账号
AddTeacherInfo addteacherinfo = new AddTeacherInfo();
addteacherinfo.textField3.setText("" + NewIndex);
addteacherinfo.textField0.setText("123456");
this.setVisible(false);
addteacherinfo.ShowFrame();
}
}
public JTextField getTextField() {
return textField;
}
public void setTextField(JTextField textField) {
this.textField = textField;
}
public JTextField getTextField1() {
return textField1;
}
public void setTextField1(JTextField textField1) {
this.textField1 = textField1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -