2020java第二次实训及导入文件、创建实体类、连接数据库

在第二次实训中,尽管进度较快,但通过详细讲稿,成功跟随并理解了代码实现。本次实训涉及创建与student数据库相关联的四个实体类,并导入辅助文件。最后实现了数据库的成功连接,带来了满足感。
摘要由CSDN通过智能技术生成

今天第二次实训,我感觉还比较好,虽然老师讲的比较快速,但是有讲稿的原因,没跟上就可以跟着讲稿理解敲代码,讲稿上的一切都比较详细。

上一次实训创建了student数据库以及stu_college,stu_status,stu_stduent,stu_user四张表,今天就对应这四张表创建了实体类。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package net.lhj.student.bean;

import java.util.Date;

public class College {
    private int id;
    private String name;
    private String president;
    private Date startTime;
    private String telephone;
    private String email;
    private String address;
    private String profile;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPresident() {
        return president;
    }

    public void setPresident(String president) {
        this.president = president;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    @Override
    public String toString() {
        return "College{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", president='" + president + '\'' +
                ", startTime=" + startTime +
                ", telephone='" + telephone + '\'' +
                ", email='" + email + '\'' +
                ", address='" + address + '\'' +
                ", profile='" + profile + '\'' +
                '}';
    }

}

这是College的代码,其他三个也差不多。

然后我们还导入了一些照片,帮助文件等
在这里插入图片描述
最后我们打了一长串关于连接数据库、关闭数据库的代码

package net.lhj.student.dbutil;

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {
    private static final String DRIVER = "com.mysql.jdbc.Driver";//数据库驱动程序
    private static final String URL = "jdbc:mysql://localhost:3306/student";//数据库统一资源标识符
    private static final String USER = "root";
    private static final String PASSWORD = "123456lhj";

    private ConnectionManager() {
    }

    public static Connection getConnection() {
        //定义数据库连接
        Connection conn = null;

        try {
            //安装数据库驱动
            Class.forName(DRIVER);
            //获取数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //返回数据库连接
        return conn;

    }

    public static void closeConnection(Connection conn) {
        //判断数据库连接是否为空
        if (conn != null) {
            try {
                //判断数据库是否已经关闭
                if (!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
    }

    public static void main(String[] args) {
        //获取数据库连接
        Connection conn = getConnection();
        //判断数据库连接是否成功
        if(conn != null){
            //提示用户数据库连接成功
            JOptionPane.showMessageDialog(null,"恭喜,数据库连接成功!");
        }else{
            JOptionPane.showMessageDialog(null,"遗憾,数据库连接失败!");
        }
        //关闭数据库连接
        closeConnection(conn);

        System.out.println("程序到此执行完毕!");
    }
}

在这里插入图片描述
看着数据库连接成功,成就感满满~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值