19103405赵宸
Java大作业实验报告
目 录
一、实验目的和要求
二、实验环境、内容和方法
三、源代码
四、调试与运行结果
五, 总结
一、实验目的和要求
1、定义一个学生基本信息管理类,有姓名、年龄、籍贯、电话号码,实现增、删、改、查4个方法。(可以使用MySql数据库实现,也可以使用集合类。)
2、在main函数中,实例化学生管理类,并根据菜单的选项分别调用4个方法。
3、该系统要求实现一个图形界面。
二、实验环境、内容和方法
实验内容:
- 点击查看信息按钮 查看数据库中全部信息
- 点击添加数据按钮 添加数据 然后点击查看信息按钮 查看数据是否添加到数据库中
- 点击删除数据按钮 输入想删除的数据 然后点击查看信息按钮 查看数据是否在数据库中被删除
- 点击更改数据按钮 输入想更改的数据 然后点击查看信息按钮 查看数据库内部的数据是否被更改了
- 点击关闭系统按钮 查看是否关闭了该程序
实验方法:
通过上机操作完成各内容。
实验环境:
笔记本电脑 Windows10操作系统 java15.0环境 mysql8.0
三、源代码
界面 与 数据库储存查找删除添加内部逻辑代码
package com.zhaochen;
import com.zhaochen.useSql.useSql;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class mainClass {
public static void main(String[] args) {
new createList();
new mainFrame("主界面");
}
}
// 学生类
class student{
private String name;
private String age;
private String province;
private String phoneNum;
public student(String name, String age, String province, String phoneNum) {
this.name = name;
this.age = age;
this.province = province;
this.phoneNum = phoneNum;
}
public student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}
//创建表类
class createList{
public createList(){
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
try {
connection = useSql.getConnection();
boolean i ;
String sql = "CREATE TABLE IF NOT EXISTS `Studens`(" +
"`name` VARCHAR(10) COMMENT '姓名'," +
"`age` VARCHAR(3) COMMENT '年龄'," +
"`province` VARCHAR(10) COMMENT '籍贯'," +
"`phoneNum` VARCHAR(11) COMMENT '电话号码'" +
")ENGINE=INNODB DEFAULT CHARSET=utf8" ;
preparedStatement = connection.prepareStatement(sql);
i = preparedStatement.execute();
if(!i){
System.out.println("已经存在这个表");
}
else{
System.out.println("创建成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
useSql.release(connection,preparedStatement,resultSet);
}
}
}
//主界面窗口
class mainFrame extends JFrame {
public mainFrame(String little) {
super(little);
Container contenter = getContentPane();
URL url = mainFrame.class.getResource("hutao.jpeg");
ImageIcon imageIcon = new ImageIcon(url);
JLabel imageJLabel = new JLabel();
imageJLabel.setLayout(null);
imageJLabel.setIcon(imageIcon);
JButton jButton1 = new JButton("学生管理系统");
jButton1.setFont(new Font("黑体",Font.BOLD,20));
jButton1.setBounds(250,0,200,50);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Caidan();
}
});
contenter.add(jButton1);
JButton jButton2 = new JButton("查看信息");
jButton2.setFont(new Font("黑体",Font.BOLD,20));
jButton2.setBounds(250,50,200,50);
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new selectFrame();
}
});
contenter.add(jButton2);
JButton jButton3 = new JButton("修改信息");
jButton3.setFont(new Font("黑体",Font.BOLD,20));
jButton3.setBounds(250,100,200,50);
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new changeFrame();
}
});
contenter.add(jButton3);
JButton jButton4 = new JButton("增添信息");
jButton4.setFont(new Font("黑体",Font.BOLD,20));
jButton4.setBounds(250,150,200,50);
jButton4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new insertFrame();
}
});
contenter.add(jButton4);
JButton jButton5 = new JButton("删除信息");
jButton5.setFont(new Font("黑体",Font.BOLD,20));
jButton5.setBounds(250,200,200,50);
jButton5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new deleteFrame();
}
});
contenter.add(jButton5);
JButton jButton6 = new JButton("制作人信息");
jButton6.setFont(new Font("黑体",Font.BOLD,20));
jButton6.setBounds(250,250,200,50);
jButton6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MakerFrame();
}
});
contenter.add(jButton6);
JButton jButton7 = new JButton("退出系统");
jButton7.setFont(new Font("黑体",Font.BOLD,20));
jButton7.setBounds(250,300,200,50);
jButton7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
contenter.add(jButton7);
imageJLabel.setOpaque(false);
contenter.add(imageJLabel);
setSize(650,400);
setLocation(400,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
//制作人窗口
class MakerFrame extends JFrame{
public MakerFrame() {
super("制作人信息");
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
setVisible(true);
setSize(300,300);
setBackground(Color.white);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(4,1));
JLabel JLabel1 = new JLabel();
JLabel1.setText("天津科技大学");
JLabel1.setFont(new Font("黑体",Font.BOLD,20));
JLabel1.setSize(200,200);
contentPane.add(JLabel1);
JLabel JLabel2 = new JLabel();
JLabel2.setText("软件工程");
JLabel2.setFont(new Font("黑体",Font.BOLD,20));
JLabel2.setSize(200,200);
contentPane.add(JLabel2);
JLabel JLabel3 = new JLabel();
JLabel3.setText("19103405");
JLabel3.setFont(new Font("黑体",Font.BOLD,20));
JLabel3.setSize(200,200);
contentPane.add(JLabel3);
JLabel JLabel4 = new JLabel();
JLabel4.setText("赵宸");
JLabel4.setFont(new Font("黑体",Font.BOLD,20));
JLabel4.setSize(200,200);
contentPane.add(JLabel4);
}
}
class Caidan extends JFrame{
public Caidan() {
super("彩蛋");
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
setVisible(true);
setSize(300,300);
Container contenter = getContentPane();
URL url = mainFrame.class.getResource("hu1.png");
ImageIcon imageIcon = new ImageIcon(url);
JLabel imageJLabel = new JLabel();
imageJLabel.setLayout(null);
imageJLabel.setIcon(imageIcon);
contenter.add(imageJLabel);
pack();
}
}
class insertFrame extends JFrame{
public insertFrame() {
super("增添数据");
setVisible(true);
setBounds(200,200,400,400);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(6,1));
JLabel jLabel1 = new JLabel("设置增添的信息");
jLabel1.setFont(new Font("黑体",Font.BOLD,20));
contentPane.add(jLabel1,GroupLayout.DEFAULT_SIZE);
JTextField textField1 = new JTextField();
textField1.setFont(new Font("黑体",Font.BOLD,30));
textField1.setText("在此输入姓名");
contentPane.add(textField1);
JTextField textField2 = new JTextField();
textField2.setFont(new Font("黑体",Font.BOLD,30));
textField2.setText("在此输入年龄");
contentPane.add(textField2);
JTextField textField3 = new JTextField();
textField3.setFont(new Font("黑体",Font.BOLD,30));
textField3.setText("在此输入籍贯");
contentPane.add(textField3);
JTextField textField4 = new JTextField();
textField4.setFont(new Font("黑体",Font.BOLD,30));
textField4.setText("在此输入电话号码");
contentPane.add(textField4);
JButton jButton = new JButton("发送信息给数据库");
jButton.setFont(new Font("黑体",Font.BOLD,20));
contentPane.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = textField1.getText();
String age = textField2.getText();
String province = textField3.getText();
String phoneNum = textField4.getText();
int i;
i = insertInformation(name,age,province,phoneNum);
if(i>0){
new insertTrue();
}
else{
new insertError();
}
textField1.setText("在此输入姓名");
textField2.setText("在此输入年龄");
textField3.setText("在此输入籍贯");
textField4.setText("在此输入电话号码");
}
});
}
public int insertInformation(String name,String age,String province,String phoneNum){
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
int row =0;
try {
int i ;
ArrayList<String> list = new ArrayList<>();
list.add(name);
list.add(age);
list.add(province);
list.add(phoneNum);
connection = useSql.getConnection();
String sql = "INSERT INTO `studens` (`name`,`age`,`province`,`phoneNum`) VALUES (?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
for (int j = 0; j < list.size(); j++) {
preparedStatement.setString(j+1,list.get(j));
}
i = preparedStatement.executeUpdate();
if(i>0){
System.out.println("插入成功!");
}
else{
System.out.println("插入失败!");
}
row = i;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
useSql.release(connection,preparedStatement,resultSet);
}
return row;
}
}
class insertTrue extends JFrame{
public insertTrue(){
super("插入成功");
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("插入成功!");
jLabel.setFont(new Font("黑体",Font.BOLD,20));
jLabel.setSize(200,200);
jLabel.setLocation(50,-25);
contentPane.add(jLabel);
}
}
class insertError extends JFrame{
public insertError(){
super("插入失败");
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("插入失败!");
jLabel.setFont(new Font("黑体",Font.BOLD,20));
jLabel.setSize(200,200);
jLabel.setLocation(50,-25);
contentPane.add(jLabel);
}
}
class deleteFrame extends JFrame{
public deleteFrame(){
super("删除信息");
setVisible(true);
setBounds(200,200,400,400);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3,1));
JLabel jLabel = new JLabel("请输入你要删除人的姓名");
jLabel.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jLabel);
JTextField jTextField = new JTextField();
jTextField.setText("在此输入你要输入人的姓名");
jTextField.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jTextField);
JButton jButton = new JButton();
jButton.setText("发送给数据库");
jButton.setFont(new Font("黑体",Font.BOLD,30));
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = jTextField.getText();
int i = deleteData(name);
if(i>0){
new deleteTrue();
}
else{
new deleteError();
}
jTextField.setText("在此输入你要输入人的姓名");
}
});
contentPane.add(jButton);
}
public int deleteData(String name){
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
int row =0;
try {
connection = useSql.getConnection();
String sql = "DELETE FROM `studens` WHERE `name` = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
row = preparedStatement.executeUpdate();
if(row >0){
System.out.println("删除成功!");
}
else{
System.out.println("删除失败!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
useSql.release(connection,preparedStatement,resultSet);
}
return row;
}
}
class deleteTrue extends JFrame{
public deleteTrue() {
super("删除成功");
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("删除成功!");
jLabel.setFont(new Font("黑体",Font.BOLD,20));
jLabel.setSize(200,200);
jLabel.setLocation(50,-25);
contentPane.add(jLabel);
}
}
class deleteError extends JFrame{
public deleteError() {
super("删除失败");
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("未找到库元素!");
jLabel.setFont(new Font("黑体",Font.BOLD,20));
jLabel.setSize(200,200);
jLabel.setLocation(25,-25);
contentPane.add(jLabel);
}
}
class changeFrame extends JFrame{
public changeFrame() {
super("修改信息");
setVisible(true);
setBounds(200,200,500,500);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(7,1));
JLabel jLabel = new JLabel("请输入你想修改谁的数据");
jLabel.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jLabel);
JTextField jTextField1 = new JTextField("在此输入想要修改人的名字");
jTextField1.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jTextField1);
JTextField jTextField2 = new JTextField("在此输入新的名字");
jTextField2.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jTextField2);
JTextField jTextField3 = new JTextField("在此输入新的年龄");
jTextField3.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jTextField3);
JTextField jTextField4 = new JTextField("在此输入新的籍贯");
jTextField4.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jTextField4);
JTextField jTextField5 = new JTextField("在此输入新的电话号码");
jTextField5.setFont(new Font("黑体",Font.BOLD,30));
contentPane.add(jTextField5);
JButton jButton = new JButton("上传至数据库");
jButton.setFont(new Font("黑体",Font.BOLD,30));
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String oldName = jTextField1.getText();
String newName = jTextField2.getText();
String newAge = jTextField3.getText();
String newProvince = jTextField4.getText();
String newPhoneNum = jTextField5.getText();
int i = updateData(oldName,newName,newAge,newProvince,newPhoneNum);
if(i>0){
new updateTrue();
}
else{
new updateError();
}
jTextField1.setText("在此输入想要修改人的名字");
jTextField2.setText("在此输入新的年龄");
jTextField3.setText("在此输入新的电话号码");
jTextField4.setText("在此输入新的籍贯");
jTextField5.setText("在此输入新的电话号码");
}
});
contentPane.add(jButton);
}
public int updateData(String oldName,String newName,String newAge,String newProvince,String newPhoneNum){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int row = 0;
try {
ArrayList<String> list = new ArrayList<>();
list.add(newName);
list.add(newAge);
list.add(newProvince);
list.add(newPhoneNum);
list.add(oldName);
int i;
connection = useSql.getConnection();
String sql = "UPDATE `studens` SET `name` =?,`age`=?,`province`=?,`phoneNum`=? WHERE `name` = ?";
preparedStatement = connection.prepareStatement(sql);
for (int j = 0; j < list.size(); j++) {
preparedStatement.setString(j+1,list.get(j));
}
i = preparedStatement.executeUpdate();
row = i;
if(i > 0){
System.out.println("修改成功!");
}
else{
System.out.println("修改失败!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return row;
}
}
class updateTrue extends JFrame{
public updateTrue() {
super("修改成功");
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("修改成功!");
jLabel.setFont(new Font("黑体",Font.BOLD,20));
jLabel.setSize(200,200);
jLabel.setLocation(50,-25);
contentPane.add(jLabel);
}
}
class updateError extends JFrame{
public updateError() {
super("修改失败");
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("未找到库元素!");
jLabel.setFont(new Font("黑体",Font.BOLD,20));
jLabel.setSize(200,200);
jLabel.setLocation(25,-25);
contentPane.add(jLabel);
}
}
class selectFrame extends JFrame{
public selectFrame() {
super("查看信息");
setVisible(true);
setBounds(200,200,200,200);
ArrayList<String> list = new ArrayList<>();
list = selectData();
String[] columnNames = new String[4];
columnNames[0] = "姓名";
columnNames[1] = "年龄";
columnNames[2] = "籍贯";
columnNames[3] = "电话号码";
String[][] rowData = new String[(list.size()/4)+1][4];
int num = 0;
for (int i = 0; i < (list.size()/4); i++) {
for (int j = 0; j < 4; j++) {
rowData[i][j] = list.get(num);
num++;
}
}
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contentPane = getContentPane();
JTable jTable = new JTable(rowData,columnNames);
jTable.setVisible(true);
jTable.setFont(new Font("黑体",Font.BOLD,15));
JScrollPane jScrollPane = new JScrollPane(jTable);
contentPane.add(jScrollPane);
pack();
}
public ArrayList<String> selectData(){
ArrayList<String> arrayList = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
student student = new student();
connection = useSql.getConnection();
String sql = "SELECT * FROM `studens`";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getString("age"));
student.setProvince(resultSet.getString("province"));
student.setPhoneNum(resultSet.getString("phoneNum"));
arrayList.add(student.getName());
arrayList.add(student.getAge());
arrayList.add(student.getProvince());
arrayList.add(student.getPhoneNum());
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
useSql.release(connection,preparedStatement,resultSet);
}
return arrayList;
}
}
mysql工具类:
package com.zhaochen.useSql;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
// 自定义的使用sql工具类
public class useSql {
private static String drive = null;
private static String url = null;
private static String username = null;
private static String password = null;
static {
try{
InputStream inputStream = useSql.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(inputStream);
drive = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//1.驱动只用加载一次
Class.forName(drive);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取链接
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection(url, username, password);
return connection;
}
//释放连接资源
public static void release(Connection con, Statement st, ResultSet re){
if(re!=null){
try {
re.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
mysql配置文件:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF8&&useSSL=true
username=root
password=123456
四、调试与运行结果
功能说明和运行截图:
启动程序 在数据库school中创建一个students表 如果存在则不用创建
主界面
查看信息
修改信息
修改成功
修改结果
修改失败的情况
增添数据
增添成功
查看一下增添的结果
删除信息
删除成功的情况
删除失败的情况
查看制作人的窗口
退出系统 直接终止程序
五、总结
通过本次java大作业的制作过程,我已经初步掌握了java的用户图形界面和如何用java操控mysql数据库的相关知识,我深刻体会到了,一个代码体的逻辑严谨的重要性,以后我们在写代码的准备过程中,需要想好整体布局,然后把整体拆成一个个小的模块,然后分模块进行对于代码的编写,最后把每一个模块的代码结合起来,最终形成一个完整的程序。