今天第二次实训,我感觉还比较好,虽然老师讲的比较快速,但是有讲稿的原因,没跟上就可以跟着讲稿理解敲代码,讲稿上的一切都比较详细。
上一次实训创建了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("程序到此执行完毕!");
}
}
看着数据库连接成功,成就感满满~