大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
2022.6.9 更新
这个课程作业的代码有小伙伴反馈有问题(数据库无法连接等),但博主最近学业不用Java了,暂时没时间看,如果有同学有解决方案,可以在下面评论帮助一下其他同学。实在抱歉啦。
package shujuku;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
import javax.swing.table.*;
class allstudent extends JFrame implements ActionListener {
JMenu jm = new JMenu("信息");
JMenuItem j1 = new JMenuItem("显示信息");
JMenuItem j2 = new JMenuItem("退出");
JMenuBar JBar = new JMenuBar();
allstudent() {
super("学生信息记录表");
setSize(540, 400);
setLocation(320, 240);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JBar.add(jm);
jm.add(j1);
jm.add(j2);
setJMenuBar(JBar);
j1.addActionListener(this);
j2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == j2)
this.dispose();
else if (e.getSource() == j1) {
int i, j, row;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException ce) {
}
try {
Connection con = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentManager", "sa", "123456");
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select * from info");
displayResultSet(rs);
rs.close();
stm.close();
//con.close();
} catch (SQLException se) {
}
}
}
public void displayResultSet(ResultSet rs) throws SQLException {
boolean moreRecords = rs.next();
if (!moreRecords) {
JOptionPane.showMessageDialog(null, "结果无记录", "无记录", JOptionPane.INFORMATION_MESSAGE);
//return;
}
Vector rows = new Vector();
Vector columnHeads = new Vector();
try {
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++)
columnHeads.addElement(rsmd.getColumnName(i));
do {
rows.addElement(getNextRow(rs, rsmd));
} while (rs.next());
JTable table = new JTable(rows, columnHeads);
table.setSize(new Dimension(400, 400));
JScrollPane scroller = new JScrollPane(table);
Container c = getContentPane();
c.add(scroller, BorderLayout.CENTER);
c.validate();
} catch (SQLException e) {
e.printStackTrace();
}
}
private Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
Vector currentRow = new Vector();
for (int i = 1; i <= rsmd.getColumnCount(); i++)
currentRow.addElement(rs.getString(i));
return currentRow;
}
// public static void main(String[] args) {
// new allstudent();
// }
}
/* * 功能:用来和数据库SQLserver进行连接,以及相应的查询方法。 */
package shujuku;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
//写一个类,用来与数据库建立连接,并且查询数据
class GetSQL {
// 设定用户名和密码
static String userword;
static String pwd;
static String english;
static String num;
static String name;
static String chinese;
static String zhengzhi;
static String math;
static String age;
static String salary;
static String sex;
static String zhicheng;
static String teanum;
static String teaname;
static Connection ct = null;
static PreparedStatement ps = null;
static ResultSet rs = null;
// 用于连接数据库的方法,可用于子类的继承
public static void ConnectSQL() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;databaseName=StudentManager","sa","123456");
System.out.println("The SQL is connected");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 用于向数据库进行查询的方法
public static void querystu(String s,String username) {
// 创建火箭车
try {
ps = ct.prepareStatement("select * from info where 权限=? and 用户名=? ");
// 给?赋值(可防止SQL注入漏洞问题),不要直接使用拼接的方式
ps.setString(1, s);
ps.setString(2, username);
// ResultSet结果集,可以把ResultSet理解成返回一张表行的结果集
rs = ps.executeQuery();
// 循环取出
if (rs.next()) {
// 将教师的用户名和密码取出
userword = rs.getString(2);
pwd = rs.getString(3);
System.out.println("成功获取到密码和用户名from数据库");
//System.out.println(userword + "\t" + pwd + "\t");
}else
{
JOptionPane.showMessageDialog(null, "没有此用户,请重新输入!", "提示消息", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//在教师表中进行查询
public static void querytea(String s,String name ) {
// 创建火箭车
try {
ps = ct.prepareStatement("select * from info_tea where 权限=? and 用户名=? ");
// 给?赋值(可防止SQL注入漏洞问题),不要直接使用拼接的方式
ps.setString(1, s);
ps.setString(2, name);
// ResultSet结果集,可以把ResultSet理解成返回一张表行的结果集
rs = ps.executeQuery();
// 循环取出
if (rs.next()) {
// 将教师的用户名和密码取出
userword = rs.getString(2);
pwd = rs.getString(3);
System.out.println("成功获取到密码和用户名from数据库");
//System.out.println(userword + "\t" + pwd + "\t");
}else
{
JOptionPane.showMessageDialog(null, "没有此用户,请重新输入!", "提示消息", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//从数据库中根据学号或者教工号来查询数据,并且填入表格。
public static void getdatastu(String s) {
// 创建火箭车
try {
ps = ct.prepareStatement("select * from info where 学号 =? ");
// 给?赋值(可防止SQL注入漏洞问题),不要直接使用拼接的方式
ps.setString(1, s);
// ResultSet结果集,大家可以把ResultSet理解成返回一张表行的结果集
rs = ps.executeQuery();
if(rs.next())
{
// 将学生的用户名和密码取出
num = rs.getString(4);
name = rs.getString(5);
math = rs.getString(6);
chinese = rs.getString(7);
english = rs.getString(8);
zhengzhi = rs.getString(9);
}else
{
JOptionPane.showMessageDialog(null, "沒有此学生,请重新输入", "提示消息", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static void getdatatea(String s) {
// 创建火箭车
try {
ps = ct.prepareStatement("select * from info_tea where 教师编号 =? ");
// 给?赋值(可防止SQL注入漏洞问题),不要直接使用拼接的方式
ps.setString(1, s);
// ResultSet结果集,大家可以把ResultSet理解成返回一张表行的结果集
rs = ps.executeQuery();
if(rs.next())
{
// 将教师的用户名和密码取出
teanum = rs.getString(4);
teaname = rs.getString(5);
sex = rs.getString(6);
salary = rs.getString(7);
zhicheng = rs.getString(8);
age = rs.getString(9);
}else
{
JOptionPane.showMessageDialog(null, "沒有此教师,请重新输入", "提示消息", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
package shujuku;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Login extends JFrame implements ActionListener {
// 定义登录界面的组件
JButton jb1, jb2, jb3 = null;
JRadioButton jrb1, jrb2 = null;
JPanel jp1, jp2, jp3, jp4 = null;
JTextField jtf = null;
JLabel jlb1, jlb2, jlb3 = null;
JPasswordField jpf = null;
ButtonGroup bg = null;
// 菜单项
JMenuBar jmb = null;
JMenu jm = null;
JMenuItem jmi1, jmi2 = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Login ms = new Login();
}
// 构造函数
public Login() {
// 创建组件
jb1 = new JButton("登录");
jb2 = new JButton("重置");
jb3 = new JButton("退出");
// 设置监听
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jmb = new JMenuBar(); // JMenuBar指菜单栏
jm = new JMenu("选项"); // JMenu是菜单栏中的选项栏
jmi1 = new JMenuItem("开始"); // JMenuItem指选项栏中的选项
jmi2 = new JMenuItem("退出系统");
jm.add(jmi1);
jm.add(jmi2);
jmb.add(jm);
jrb1 = new JRadioButton("教师", true);
jrb2 = new JRadioButton("学生");
bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
// jrb2.setSelected(true);
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jlb1 = new JLabel("用户名:");
jlb2 = new JLabel("密 码:");
jlb3 = new JLabel("权 限:");
jtf = new JTextField(10);
jpf = new JPasswordField(10);
// 加入到JPanel中
jp1.add(jlb1);
jp1.add(jtf);
jp2.add(jlb2);
jp2.add(jpf);
jp3.add(jlb3);
jp3.add(jrb1);
jp3.add(jrb2);
jp4.add(jb1);
jp4.add(jb2);
jp4.add(jb3);
// 加入JFrame中
this.setJMenuBar(jmb);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
// 设置布局管理器
this.setLayout(new GridLayout(4, 1));
// 给窗口设置标题
this.setTitle("学生成绩管理系统");
// 设置窗体大小
this.setSize(300, 250);
// 设置窗体初始位置
this.setLocation(200, 150);
// 设置当关闭窗口时,保证JVM也退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 显示窗体
this.setVisible(true);
this.setResizable(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "退出") {
System.exit(0);
} else if (e.getActionCommand() == "登录") {
if (!jtf.getText().isEmpty() && !jpf.getText().isEmpty()) {
// 当点击登录按钮时,首先与数据库建立连接
GetSQL.ConnectSQL();
// 如果选中教师登录
if (jrb1.isSelected()) {
GetSQL.querytea("教师", jtf.getText());
// 首先判断是否存在该用户,即是否得到了密码
if (GetSQL.pwd == null) {
this.clear();
} else {
// 调用登录方法
this.tealogin();
}
} else if (jrb2.isSelected()) // 学生在登录系统
{
GetSQL.querystu("学生", jtf.getText());
// 首先判断是否存在该用户,即是否得到了密码
if (GetSQL.pwd == null) {
this.clear();
} else {
// 调用登录方法
this.stulogin();
}
}
} else if (jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名", "提示消息", JOptionPane.WARNING_MESSAGE);
this.clear();
} else if (jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入密码", "提示消息", JOptionPane.WARNING_MESSAGE);
this.clear();
}
} else if (e.getActionCommand() == "重置") {
this.clear();
}
}
// 清空文本框和密码框
public void clear() {
jtf.setText("");
jpf.setText("");
}
// 学生登录判断方法
public void stulogin() {
if (GetSQL.pwd.equals(jpf.getText())) {
// System.out.println("登录成功");
JOptionPane.showMessageDialog(null, "登录成功!", "提示消息", JOptionPane.WARNING_MESSAGE);
this.clear();
// 关闭当前界面
dispose();
// 创建一个新界面
Stu_UI ui = new Stu_UI();
} else if (jtf.getText().isEmpty() && jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名和密码!", "提示消息", JOptionPane.WARNING_MESSAGE);
} else if (jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名!", "提示消息", JOptionPane.WARNING_MESSAGE);
} else if (jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入密码!", "提示消息", JOptionPane.WARNING_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
// 清空输入框
this.clear();
}
}
// 教师登录判断方法
public void tealogin() {
if (GetSQL.pwd.equals(jpf.getText())) {
// System.out.println("登录成功");
JOptionPane.showMessageDialog(null, "登录成功!", "提示消息", JOptionPane.WARNING_MESSAGE);
this.clear();
// 关闭当前界面
dispose();
// 创建一个新界面,适用于教师来管理学生
Teacher t = new Teacher();
} else if (jtf.getText().isEmpty() && jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名和密码!", "提示消息", JOptionPane.WARNING_MESSAGE);
} else if (jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名!", "提示消息", JOptionPane.WARNING_MESSAGE);
} else if (jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入密码!", "提示消息", JOptionPane.WARNING_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
// 清空输入框
this.clear();
}
}
}
/* * 功能:学生登录成功界面。 * */
package shujuku;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
public class Stu_UI extends JFrame implements ActionListener
{
//定义组件
JMenuBar jm= new JMenuBar();
JMenu jm1 = new JMenu("查询");
JMenuItem jmi1 = new JMenuItem("基本信息");
JMenu jm2 = new JMenu("修改");
JMenuItem jmi2 = new JMenuItem("修改密码");
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// Stu_UI ui=new Stu_UI();
// }
//构造函数
public Stu_UI() //不能申明为void!!!!!否则弹不出新界面
{
//创建组件
this.setTitle("学生管理系统");
this.setLayout(new CardLayout());
this.setJMenuBar(jm);
jm.add(jm1);
jm.add(jm2);
jm1.add(jmi1);
jm2.add(jmi2);
jmi1.addActionListener(this);
jmi2.addActionListener(this);
this.setSize(400,300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jmi1){
find();
}
if(e.getSource()==jmi2){
change();
}
}
public void find(){
final JDialog fontDialog = new JDialog(this, "基本信息", false);
Container con = fontDialog.getContentPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(5,2));
JPanel panel2 = new JPanel();
JLabel J1 = new JLabel("请输入学号:");
final JTextField JT1 = new JTextField(15);
JLabel math = new JLabel("数学:");
final JTextField math1 = new JTextField(15);
JLabel chinese = new JLabel("语文:");
final JTextField chinese1 = new JTextField(15);
JLabel eng = new JLabel("英语:");
final JTextField eng1 = new JTextField(15);
JLabel zhengzhi = new JLabel("政治:");
final JTextField zhengzhi1 = new JTextField(15);
JButton JB1 = new JButton("查询");
panel1.add(J1);
panel1.add(JT1);
panel1.add(math);
panel1.add(math1);
panel1.add(chinese);
panel1.add(chinese1);
panel1.add(eng);
panel1.add(eng1);
panel1.add(zhengzhi);
panel1.add(zhengzhi1);
panel2.add(JB1);
con.add(panel1);
con.add(panel2);
con.setLayout(new GridLayout(2,1));
fontDialog.setSize(400, 400);
fontDialog.setLocation(200, 200);
fontDialog.setResizable(true);
fontDialog.setVisible(true);
JB1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch (ClassNotFoundException ce){
}
try
{
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentManager","sa","123456");
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select * from info where 学号= '" + JT1.getText()+ "'");
if(rs.next()){
math1.setText(rs.getString("数学"));
chinese1.setText(rs.getString("语文"));
eng1.setText(rs.getString("英语"));
zhengzhi1.setText(rs.getString("政治"));
}
else {
JOptionPane.showMessageDialog(null,"不存在该记录!");}
}
catch (SQLException se)
{
JOptionPane.showMessageDialog(null,se.getMessage()); }
}
});
}
public void change(){
final JDialog fontDialog = new JDialog(this, "修改个人密码", false);
Container con = fontDialog.getContentPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,2));
JPanel panel2 = new JPanel();
JLabel JLP = new JLabel("请输入你的学号:");
final JTextField JTL = new JTextField();
JLabel JLP1 = new JLabel("请输入原来密 码 : ");
JPasswordField JTP1 = new JPasswordField();
JLabel JLP2 = new JLabel("请输入新密 码 : ");
JPasswordField JTP2 = new JPasswordField();
JLabel JLP3 = new JLabel("请再次输入新密 码 : ");
final JPasswordField JTP3 = new JPasswordField();
JButton JB1 = new JButton("确定");
JButton JB2 = new JButton("退出");
panel1.add(JLP);
panel1.add(JTL);
panel1.add(JLP1);
panel1.add(JTP1);
panel1.add(JLP2);
panel1.add(JTP2);
panel1.add(JLP3);
panel1.add(JTP3);
panel2.add(JB1);
panel2.add(JB2);
con.setLayout(new GridLayout(2,1));
con.add(panel1);
con.add(panel2);
fontDialog.setSize(400, 340);
fontDialog.setLocation(200, 200);
fontDialog.setResizable(false);
fontDialog.setVisible(true);
JB1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
JTL.setEditable(false);
Connection cot = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentManager","sa","123456");
Statement stmt = cot.createStatement();
int a = stmt.executeUpdate("Update info set 密码='"+JTP3.getText()+"'where 学号='"+JTL.getText()+"'");
//System.out.print("成功了");
if(a==1){
JOptionPane.showMessageDialog(null,"修改成功");}
else
{
JOptionPane.showMessageDialog(null,"修改失败");}
}catch(SQLException se){
System.out.print("数据连接或修改失败"); }
}
});
}
}
/* * 功能:实现了查询修改删除学生的信息。 */
package shujuku;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
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.sql.Statement;
public class Teacher extends JFrame implements ActionListener {
// 定义组件
JMenuBar mainMenu = new JMenuBar();
JMenu menu1 = new JMenu("学生信息操作");
JMenuItem itemAdd = new JMenuItem("添加学生信息");
JMenuItem itemDel = new JMenuItem("删除学生信息");
JMenuItem itemMod = new JMenuItem("修改学生信息");
JMenu itemSel = new JMenu("查看学生信息");
JMenuItem item1 = new JMenuItem("查看全部学生");
JMenuItem item2 = new JMenuItem("查看指定学生");
JPanel jp = new JPanel();
// 构造函数
public Teacher() {
// 创建组件
super("学生管理系统-老师界面");
mainMenu.add(menu1);
mainMenu.add(itemSel);
menu1.add(itemAdd);
menu1.add(itemDel);
menu1.add(itemMod);
//menu1.add(itemSel);
itemSel.add(item1);
itemSel.add(item2);
itemAdd.addActionListener(this);
itemDel.addActionListener(this);
itemMod.addActionListener(this);
itemSel.addActionListener(this);
item1.addActionListener(this);
item2.addActionListener(this);
this.setSize(320, 240);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(mainMenu);
this.add(jp);
this.setLayout(null);
this.setTitle("学生成绩管理系统—教师");
this.setSize(500, 500);
this.setLocation(150, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public void Add() {
final JDialog fontDialog = new JDialog(this, "添加学生信息", false);
Container con = fontDialog.getContentPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2));
JPanel panel2 = new JPanel();
JLabel JLNumber = new JLabel("学号:");
final JTextField JTNumber = new JTextField(15);
JLabel JLName = new JLabel("姓名:");
final JTextField JTName = new JTextField(15);
JLabel JLusername = new JLabel("用户名:");
final JTextField JTuname = new JTextField(15);
JLabel JLpassword = new JLabel("密码:");
final JTextField JTpassword = new JTextField(15);
JButton JBAdd = new JButton("添加");
JButton JBNext = new JButton("重置");
panel1.add(JLNumber);
panel1.add(JTNumber);
panel1.add(JLName);
panel1.add(JTName);
panel1.add(JLusername);
panel1.add(JTuname);
panel1.add(JLpassword);
panel1.add(JTpassword);
panel2.add(JBAdd);
panel2.add(JBNext);
con.setLayout(new GridLayout(2, 1));
con.add(panel1);
con.add(panel2);
fontDialog.setSize(400, 340);
fontDialog.setLocation(200, 200);
fontDialog.setResizable(true);
fontDialog.setVisible(true);
JBAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException ce) {
JOptionPane.showMessageDialog(null, ce.getMessage());
}
try {
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=StudentManager", "sa", "123456");
Statement stm = con.createStatement();
int a = stm.executeUpdate("insert into info(权限,用户名,密码,学号,姓名) "+" values ('学生'"+",'"+JTuname.getText()+"','"+JTpassword.getText()+"','"+JTNumber.getText()+"','"+JTName.getText()+"')");
if (a == 1) {
JOptionPane.showMessageDialog(null, "添加成功!");
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
stm.close();
} catch (Exception ee) {
JOptionPane.showMessageDialog(null, ee.getMessage());
}
}
});
JBNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTNumber.setText("");
JTName.setText("");
JTuname.setText("");
JTpassword.setText("");
}
});
}
public void Select() {
final JDialog fontDialog = new JDialog(this, "学生信息", false);
Container con = fontDialog.getContentPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(6, 2));
JPanel panel2 = new JPanel();
JLabel J1 = new JLabel("请输入学号:");
final JTextField JT1 = new JTextField("");
JLabel Jname = new JLabel("姓名:");
final JTextField JTname = new JTextField("");
JLabel JLmath = new JLabel("数学:");
final JTextField JTmath = new JTextField();
JLabel JLchinese = new JLabel("语文:");
final JTextField JTchinese = new JTextField();
JLabel JLeng = new JLabel("英语:");
final JTextField JTeng = new JTextField(15);
JLabel JLzhengzhi = new JLabel("政治:");
final JTextField JTzhengzhi = new JTextField(15);
JButton JB1 = new JButton("查询");
JButton JB2 = new JButton("重置");
JButton JB3 = new JButton("删除");
JButton JB4 = new JButton("修改");
panel1.add(J1);
panel1.add(JT1);
panel1.add(Jname);
panel1.add(JTname);
panel1.add(JLmath);
panel1.add(JTmath);
panel1.add(JLchinese);
panel1.add(JTchinese);
panel1.add(JLeng);
panel1.add(JTeng);
panel1.add(JLzhengzhi);
panel1.add(JTzhengzhi);
panel2.add(JB1);
panel2.add(JB2);
panel2.add(JB3);
panel2.add(JB4);
con.setLayout(new GridLayout(2, 1));
con.add(panel1);
con.add(panel2);
fontDialog.setSize(400, 340);
fontDialog.setLocation(200, 200);
fontDialog.setResizable(true);
fontDialog.setVisible(true);
JB1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException ce) {
}
try {
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=StudentManager", "sa", "123456");
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select * from info where 学号= '" + JT1.getText() + "'");
if (rs.next()) {
JTname.setText(rs.getString("姓名"));
JTmath.setText(rs.getString("数学"));
JTchinese.setText(rs.getString("语文"));
JTeng.setText(rs.getString("英语"));
JTzhengzhi.setText(rs.getString("政治"));
} else {
JOptionPane.showMessageDialog(null, "不存在该记录!");
}
} catch (SQLException se) {
JOptionPane.showMessageDialog(null, se.getMessage());
}
}
});
JB2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JT1.setText(null);
JTmath.setText("");
JTchinese.setText("");
JTeng.setText("");
JTzhengzhi.setText("");
JT1.requestFocus();
}
});
JB3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=StudentManager", "sa", "123456");
Statement stmt = con.createStatement();
int a = stmt.executeUpdate("delete from info where 学号='" + JT1.getText() + "' ");
if (a == 1) {
JOptionPane.showMessageDialog(null, "删除成功!");
} else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
JTmath.setText("");
JTchinese.setText("");
JTeng.setText("");
JTzhengzhi.setText("");
con.close();
} catch (SQLException ee) {
}
}
});
JB4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=StudentManager", "sa", "123456");
Statement stmt = con.createStatement();
int a = stmt.executeUpdate("Update info set 数学='"+JTmath.getText()+"'," +
"语文='"+JTchinese.getText()+"',"+"英语='"+JTeng.getText()+"',"+"政治="+JTzhengzhi.getText()+"where 学号='"+JT1.getText()+"'");
stmt = con.createStatement();
//System.out.println("chenggong");
if (a == 1) {
JOptionPane.showMessageDialog(null, "修改成功");
} else {
JOptionPane.showMessageDialog(null, "修改失败");
}
JTmath.setText("");
JTchinese.setText("");
JTeng.setText("");
JTzhengzhi.setText("");
con.close();
} catch (SQLException se) {
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == itemAdd) {
Add();
} else if (e.getSource() == item1) {
new allstudent();
} else {
Select();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Teacher();
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/181798.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...