文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 第一个版本
- 第二个版本
第一个版本
因为代码有点长 就发了部分。
package StudentManager.test;
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JTabletest extends JFrame{
public JTabletest() throws SQLException {
Connection connection=null;/
PreparedStatement preparedStatement=connection.prepareStatement("select *from student");//sql语句编译预处理
ResultSet res=preparedStatement.executeQuery();//获取ResultSet结果集
res.last();//游标移动到查询到的数据库数据记录的最后一条
int row=res.getRow();//获取记录的总条数
res.beforeFirst();//游标回到第一个记录前的位置
String arr[]= {"姓名","年龄","专业","简介"};//定义表格的列名称
String comm[][] = new String[row][4];//row行,4列
res.next();//游标回到第一个记录的位置
for(int i=0;i
comm[i][0]=res.getString("name");
comm[i][1]=res.getString("年龄");
comm[i][2]=res.getString("专业");
comm[i][3]=res.getString("简介");
res.next();
}
JTable jTable=new JTable(comm,arr);
jTable.setPreferredScrollableViewportSize(new Dimension(800,200));//设置可滚动视图的大小
JScrollPane jScrollPane=new JScrollPane(jTable);
add(jScrollPane,BorderLayout.CENTER);//将滚动条放到窗体
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);//设置窗体可见
validate();//设置组件可见
pack();//自动调整组建大小使每个组键都能显示
connection.close();
}
}
package StudentManager.test;
import StudentManager.AddFrame;
import StudentManager.ChangeFrame;
import StudentManager.DeleteFrame;
import StudentManager.SelectFrame;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class MainFrame extends JFrame {
private JPanel contentPane;
// 有了main函数才可以单独运行
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
// 设置大小不可改变
setResizable(false);
// 设置标题
setTitle("学生管理系统");
// 由于这是主页面,所有当主页面关闭的时候,程序就直接退出
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(int x,int y,int width,int height);
// x 和 y 是窗口打开时的位置
// width 和 height 是窗口打宽度和高度
setBounds(100, 100, 450, 350);
// setLocationRelativeTo(c)
// 设置窗口相对于 c 的位置,当 c 为空或者 null 时,默认为是相对于屏幕中央
setLocationRelativeTo(null);
// 实例化一个 pane
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("学生管理系统");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 25));
lblNewLabel.setBounds(140, 10, 163, 44);
contentPane.add(lblNewLabel);
JButton addButton = new JButton("添加学生");
// addActionListener 注册监听器
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 设置新窗口可见
new AddFrame().setVisible(true);
}
});
addButton.setFont(new Font("宋体", Font.PLAIN, 18));
addButton.setBounds(167, 64, 114, 37);
contentPane.add(addButton);
JButton changeButton = new JButton("修改信息");
changeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 同上
new ChangeFrame().setVisible(true);
}
});
changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
changeButton.setBounds(167, 121, 114, 37);
contentPane.add(changeButton);
JButton deleteButton = new JButton("删除学生");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 同上
new DeleteFrame().setVisible(true);
}
});
deleteButton.setFont(new Font("宋体", Font.PLAIN, 18));
deleteButton.setBounds(167, 180, 114, 37);
contentPane.add(deleteButton);
JButton queryButton = new JButton("查询个人信息");
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 同上
try {
new QueryFrame().setVisible(true);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
queryButton.setFont(new Font("宋体", Font.PLAIN, 18));
queryButton.setBounds(167, 240, 114, 37);
contentPane.add(queryButton);
JButton selectButton = new JButton("查询所有学生记录");
selectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
SelectFrame s = new SelectFrame();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
}
}
package StudentManager.test;
import java.sql.SQLException;
public class mysqltest {
public static void main(String[] args) throws SQLException {
JTabletest j = new JTabletest();
}
}
package StudentManager.test;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class QueryFrame extends JFrame {
public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";//版本低的可以删除中间得cj.
public static final String DB_URL = "jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//在问好之后的东西都是一些去重连接中可能会报错的地方
public static final String username = "root"; //数据库账号
public static final String password = "111111";//数据库密码
public QueryFrame() throws SQLException {
JTable resultarea = new JTable();
Connection connection=null;/
connection = DriverManager.getConnection(DB_URL,username,password);
PreparedStatement preparedStatement=connection.prepareStatement("select *from student");//sql语句编译预处理
ResultSet res=preparedStatement.executeQuery();//获取ResultSet结果集
res.last();//游标移动到查询到的数据库数据记录的最后一条
int row=res.getRow();//获取记录的总条数
res.beforeFirst();//游标回到第一个记录前的位置
String arr[]= {"姓名","年龄","专业","简介"};//定义表格的列名称
String comm[][] = new String[row][4];//row行,4列
res.next();//游标回到第一个记录的位置
for(int i=0;i
comm[i][0]=res.getString("name");
comm[i][1]=res.getString("age");
comm[i][2]=res.getString("grade");
comm[i][3]=res.getString("introduction");
res.next();
}
JTable jTable=new JTable(comm,arr);
jTable.setPreferredScrollableViewportSize(new Dimension(800,200));//设置可滚动视图的大小
JScrollPane jScrollPane=new JScrollPane(jTable);
add(jScrollPane,BorderLayout.CENTER);//将滚动条放到窗体
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);//设置窗体可见
validate();//设置组件可见
pack();//自动调整组建大小使每个组键都能显示
connection.close();
}
}
第二个版本
这个相当于是一个功能更加完善的系统,后续功能还未开发好~后续将会补上



