package dao;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;
public class ClassManager extends JFrame{
Container c = getContentPane();
private Statement stm;
private ResultSet rs;
private JTable table;
private JScrollPane js;
private JTextField text1,text2,text3;
private JPanel jp;
private int count,id;
private JLabel change1,change2,change3,change4;
private Connections connections;
public ClassManager(){
super();
//标题
setTitle("班级管理");
//设置布局
setLayout(null);
//不可放大缩小
setResizable(false);
//设置窗口大小
setBounds(570,260,680,520);
//可关闭
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//调用方法
search();
data();
//可视化
setVisible(true);
}
public void search(){
jp = new JPanel();
jp.setLayout(null);
jp.setBounds(0,0,680,520);
//组件
JLabel jl = new JLabel("班级管理");
//设置字体
Font font = new Font("宋体",Font.BOLD,24);
jl.setFont(font);
jl.setBounds(270, 10, 200, 40);
JLabel jl1 = new JLabel("请输入班级编号:");
jl1.setBounds(150,50,150,30);
text1 = new JTextField("",13);
text1.setBounds(250,50,100,25);
c.add(jl1);
c.add(text1);
//按钮监听
JButton btn1 = new JButton("查询");
btn1.setBounds(380,49,60,25);
btn1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
find();
}
});
c.add(btn1);
JButton btn2 = new JButton("更新");
btn2.setBounds(530,49,60,25);
btn2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//调用方法
update();
}
});
JButton btn3 = new JButton("删除");
btn3.setBounds(600,49,60,25);
btn3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
delete();
}
});
JButton btn4 = new JButton("添加");
btn4.setBounds(460,49,60,25);
btn4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
add();
}
});
///表格头
String[] columnName=
{"班号","名字"};
String[][] tableValues =new String[20][columnName.length];
table = new JTable(tableValues,columnName);
table.setBounds(30,100,620,320);
table.getTableHeader().setReorderingAllowed(false);
Enumeration<TableColumn> cms = table.getColumnModel().getColumns();
while(cms.hasMoreElements()){
cms.nextElement().setPreferredWidth(200);
}
//表格加入滚动条
js = new JScrollPane(table);
js.setBounds(30,90,610,320);
jp.add(js);
//面板添加组件
jp.add(btn2);
jp.add(btn4);
jp.add(btn3);
jp.add(jl);
c.add(jp);
}
public void data(){
try {
//获取数据连接
connections = new Connections();
Connection con = connections.getConnection();
stm = (Statement) con.createStatement();
Statement stm1 = (Statement) con.createStatement();
//获取数据库数据
rs = stm.executeQuery("select * from classes");
ResultSet rs1 = stm1.executeQuery("select count(*) from classes");
while(rs1.next()){
count = rs1.getInt(1);
}
//把数据放进集合
ArrayList save = new ArrayList();
while(rs.next()){
String Str1 = rs.getString(1);
String Str2 = rs.getString(2);
save.add(Str1);
save.add(Str2);
}
//取出数据
Iterator iterator = save.iterator();//迭代器遍历里面元素
while(iterator.hasNext()){
//把数据设置进表格
for(int i =0;i<count;i++){
for(int j = 0;j<2;j++){
String data = (String) iterator.next();
table.setValueAt(data, i, j);
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void find(){
//把表格所有数据清空
try {
for(int i =0;i<count+2;i++){
for(int j = 0;j<2;j++){
table.setValueAt("", i, j);
}
}
connections = new Connections();
Connection con = connections.getConnection();
stm = (Statement) con.createStatement();
Statement stm1 = (Statement) con.createStatement();
//获取输入的数据
String t1 = text1.getText();
rs = stm.executeQuery("select * from classes where id='"+t1+"'");
ResultSet rs1 = stm1.executeQuery("select count(*) from classes where id='"+t1+"'");
while(rs1.next()){
count = rs1.getInt(1);
}
//把数据放进集合
ArrayList save = new ArrayList();
while(rs.next()){
String Str1 = rs.getString(1);
String Str2 = rs.getString(2);
save.add(Str1);
save.add(Str2);
}
//取出数据
Iterator iterator = save.iterator();//迭代器遍历里面元素
while(iterator.hasNext()){
//把数据设置进表格
for(int i =0;i<count;i++){
for(int j = 0;j<2;j++){
String data = (String) iterator.next();
table.setValueAt(data, i, j);
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void add(){
try {
//获取数据库连接
connections = new Connections();
Connection con = connections.getConnection();
stm = (Statement) con.createStatement();
//获取表格选择行
int index = table.getSelectedRow();
//获取选择行数据
String ISBN = (String) table.getValueAt(index, 0);
String name = (String) table.getValueAt(index, 1);
//添加进数据库
String sql = ("insert into classes values('"+ISBN+"','"+name+"')");
int resule2 = stm.executeUpdate(sql);
if(resule2==1){
//添加成功则弹出弹窗
JOptionPane.showMessageDialog(null,"添加成功","确认",JOptionPane.PLAIN_MESSAGE);
}
stm.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,"编号相同","确认",JOptionPane.PLAIN_MESSAGE);
}
}
public void update(){
try {
//连接数据库
connections = new Connections();
Connection con = connections.getConnection();
stm = (Statement) con.createStatement();
int index = table.getSelectedRow();
//获取表格数据
String ISBN = (String) table.getValueAt(index, 0);
String name = (String) table.getValueAt(index, 1);
//执行数据库语句
String sql = ("update classes set name='"+name+"' where id='"+ISBN+"'");
int resule2 = stm.executeUpdate(sql);
if(resule2==1){
//数据库成功修改数据后弹出对话框
JOptionPane.showMessageDialog(null,"更新成功","确认",JOptionPane.PLAIN_MESSAGE);
}
stm.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete(){
try {
//获取数据库数据
connections = new Connections();
Connection con = connections.getConnection();
stm = (Statement) con.createStatement();
//获取表格数据
int index = table.getSelectedRow();
String ISBN = (String) table.getValueAt(index, 0);
//删除数据库语句
String sql = ("delete from classes where id='"+ISBN+"'");
int resule2 = stm.executeUpdate(sql);
if(resule2==1){
//删除成功弹出对话框
JOptionPane.showMessageDialog(null,"删除成功","确认",JOptionPane.PLAIN_MESSAGE);
}
stm.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//表格数据全部清空
for(int i =0;i<count+2;i++){
for(int j = 0;j<2;j++){
table.setValueAt("", i, j);
}
}
//表格重新刷新数据
data();
}
}
public static void main(String[] args){
ClassManager user = new ClassManager();
}
}
能给解释一下每一块具体是干嘛的吗实在是不理解
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 猿码叔叔 2022-01-11 08:50关注
ClassManager() 构造函数的作用是初始化该类时,同时利用JPanel相关组件生成一个可视窗口用于数据操作;
data 方法是填充数据,相当于页面默认加载需要的数据。需要去数据库查询列表然后回显至刚才生成的那个窗口
search 方法动态渲染了搜索需要的相关页面元素,必须搜索按钮或搜索框;
其他比如 add 对应增加数据、update 对应修改数据、find 对应搜索数据、delete 对应删除数据;
码字不易,还望收纳!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 做个有关计算的小程序
- ¥15 MPI读取tif文件无法正常给各进程分配路径
- ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
- ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
- ¥15 setInterval 页面闪烁,怎么解决
- ¥15 如何让企业微信机器人实现消息汇总整合
- ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
- ¥15 如何用Python爬取各高校教师公开的教育和工作经历
- ¥15 TLE9879QXA40 电机驱动
- ¥20 对于工程问题的非线性数学模型进行线性化