第二次项目实训完成了第6-8实训内容
文章目录(项目6)
实现步骤
(二)创建项目
1、创建Java项目
2、在项目里创建文件夹
3、在lib里添加MySQL数据库驱动程序包
4、在images里添加图片
5、在help里添加帮助文档
(二)创建项目
1、创建Java项目
创建Java项目 - 学生信息管理系统(MySQL版)V1.0
2、在项目里创建文件夹 3、在lib里添加MySQL数据库驱动程序包
下载数据库驱动程序包 链接:https://pan.baidu.com/s/1xE-jJVMGiPQ5Qdxi7q-KZw 提取码:og6p
将驱动程序包拷贝到lib目录
作为库添加到项目里(Add as Library…) 右键点击驱动程序包点击——Add as Library… 4、在images里添加图片
下载图片素材
链接:https://pan.baidu.com/s/1V3afx3Lw_4sw3R2Gyy2mZg 提取码:4sjd
将图片素材拷贝到images目录
用PS打开背景图片background.jpg,添加个人信息:班级 - 学号 - 姓名 5、在help里添加帮助文档
下载帮助文档
链接:https://pan.baidu.com/s/11TG9x3dfxej-06eWbcsqGw 提取码:iv6z 将帮助文档拷贝到help目录
文章目录(项目7)
实现步骤
(三)创建实体类
1、创建学校实体类对应学校表
2、创建状态实体类对应状态表
3、创建学生实体类对应学生表
4、创建用户实体类对应用户表
(三)创建实体类
在src里创建net.zm.student.bean包
依次创建四个实体类:College、Status、Student与User,分别对应t_college表、t_status表、t_student表与t_user表。
实体类的属性对应于数据表的字段,主要两者的数据类型要匹配,网上可以搜索到MySQL数据类型与Java数据类型的匹配对应表。
1、创建学校实体类对应学校表
package net
.zm
.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
+ '\'' +
'}';
}
}
注意:导入的日期类是util包里的Date类,import java.util.Date; 不要导成sql包里的Date类。
2、创建状态实体类对应状态表
package net
.zm
.student
.bean
;
public class Status {
private int id
;
private String college
;
private String version
;
private String author
;
private String telephone
;
private String address
;
private String email
;
public int getId() {
return id
;
}
public void setId(int id
) {
this.id
= id
;
}
public String
getCollege() {
return college
;
}
public void setCollege(String college
) {
this.college
= college
;
}
public String
getVersion() {
return version
;
}
public void setVersion(String version
) {
this.version
= version
;
}
public String
getAuthor() {
return author
;
}
public void setAuthor(String author
) {
this.author
= author
;
}
public String
getTelephone() {
return telephone
;
}
public void setTelephone(String telephone
) {
this.telephone
= telephone
;
}
public String
getAddress() {
return address
;
}
public void setAddress(String address
) {
this.address
= address
;
}
public String
getEmail() {
return email
;
}
public void setEmail(String email
) {
this.email
= email
;
}
@Override
public String
toString() {
return "Status{" +
"id=" + id
+
", college='" + college
+ '\'' +
", version='" + version
+ '\'' +
", author='" + author
+ '\'' +
", telephone='" + telephone
+ '\'' +
", address='" + address
+ '\'' +
", email='" + email
+ '\'' +
'}';
}
}
3、创建学生实体类对应学生表
package net
.zm
.student
.bean
;
public class Student {
private String id
;
private String name
;
private String sex
;
private int age
;
private String department
;
private String clazz
;
private String telephone
;
public String
getId() {
return id
;
}
public void setId(String id
) {
this.id
= id
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public String
getSex() {
return sex
;
}
public void setSex(String sex
) {
this.sex
= sex
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
public String
getDepartment() {
return department
;
}
public void setDepartment(String department
) {
this.department
= department
;
}
public String
getClazz() {
return clazz
;
}
public void setClazz(String clazz
) {
this.clazz
= clazz
;
}
public String
getTelephone() {
return telephone
;
}
public void setTelephone(String telephone
) {
this.telephone
= telephone
;
}
@Override
public String
toString() {
return "Student{" +
"id='" + id
+ '\'' +
", name='" + name
+ '\'' +
", sex='" + sex
+ '\'' +
", age=" + age
+
", department='" + department
+ '\'' +
", clazz='" + clazz
+ '\'' +
", telephone='" + telephone
+ '\'' +
'}';
}
}
4、创建用户实体类对应用户表
package net
.zm
.student
.bean
;
import java
.sql
.Timestamp
;
public class User {
private int id
;
private String username
;
private String password
;
private String telephone
;
private Timestamp registerTime
;
public int getId() {
return id
;
}
public void setId(int id
) {
this.id
= id
;
}
public String
getUsername() {
return username
;
}
public void setUsername(String username
) {
this.username
= username
;
}
public String
getPassword() {
return password
;
}
public void setPassword(String password
) {
this.password
= password
;
}
public String
getTelephone() {
return telephone
;
}
public void setTelephone(String telephone
) {
this.telephone
= telephone
;
}
public Timestamp
getRegisterTime() {
return registerTime
;
}
public void setRegisterTime(Timestamp registerTime
) {
this.registerTime
= registerTime
;
}
@Override
public String
toString() {
return "User{" +
"id=" + id
+
", username='" + username
+ '\'' +
", password='" + password
+ '\'' +
", telephone='" + telephone
+ '\'' +
", registerTime=" + registerTime
+
'}';
}
}
注意:导入的日期类是util包里的Date类,import java.util.Date; 不要导成sql包里的Date类。
文章目录(项目8)
实现步骤
(四)创建数据库连接管理类方法
1、在程序里定义数据库连接属性常量方法
2、从数据库配置文件里读取属性值
(四)创建数据库连接管理类方法
在程序里定义数据库连接属性常量,也可以将数据库连接属性值写入数据库配置文件,并且保存在项目内,在程序中读取文件中的信息,拿到数据库连接属性值,从而进行数据库连接。
方法1、在程序里定义数据库连接属性常量
在src里创建net.zm.student.dbutil包 在包里创建数据库连接管理类ConnectionManager
package net
.zm
.student
.dbutli
;
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
= "1";
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
);
}
}
说明:private static final String PASSWORD = “root”; 密码值改成你电脑上MySQL的密码。
运行程序,测试数据库连接是否成功
文章目录(Java讲课笔记31:JDBC入门)
实现步骤
实现数据的增删改
1.创建DisplayAllUsers显示全部用户记录类(使用普通语句对象)
2.创建FindUserById按编号查找用户记录类
3.创建Login用户登录类(使用预备语句对象)
4.创建AddUser添加用户记录类
5.创建UpdateUser更新用户记录类
6.创建DeleteUserById删除用户记录类
步骤实现
在net.zm.student包下创建test文件包
1.创建显示全部用户记录类
创建DisplayAllUsers(使用普通语句对象
package net
.zm
.student
.test
;
import net
.zm
.student
.bean
.User
;
import net
.zm
.student
.dbutli
.ConnectionManager
;
import java
.sql
.Connection
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;
public class DisplayAIIUsers {
public static void main(String
[] args
) {
Connection conn
= ConnectionManager
.getConnection();
try {
String strSQL
= "select * from t_user";
Statement stmt
= conn
.createStatement();
ResultSet rs
= stmt
.executeQuery(strSQL
);
while (rs
.next()) {
User user
= new User();
user
.setId(rs
.getInt("id"));
user
.setUsername(rs
.getString("username"));
user
.setPassword(rs
.getString("password"));
user
.setTelephone(rs
.getString("telephone"));
user
.setRegisterTime(rs
.getTimestamp("register_time"));
System
.out
.println(user
);
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
ConnectionManager
.closeConnection(conn
);
}
}
}
运行程序,查看结果
2.创建FindUserById按编号查找用户记录类
package net
.zm
.student
.test
;
import net
.zm
.student
.dbutli
.ConnectionManager
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class FindUserById {
public static void main(String
[] args
) {
int id
;
Scanner sc
= new Scanner(System
.in
);
System
.out
.print("请输入待查用户编号:");
id
= sc
.nextInt();
Connection conn
= ConnectionManager
.getConnection();
try {
String strSQL
= "select * from t_user where id = ?";
PreparedStatement pstmt
= conn
.prepareStatement(strSQL
);
pstmt
.setInt(1,id
);
ResultSet rs
= pstmt
.executeQuery();
if (rs
.next()){
System
.out
.println("编号:"+rs
.getInt("id"));
System
.out
.println("用户名:"+rs
.getString("username"));
System
.out
.println("密码:"+rs
.getString("password"));
System
.out
.println("电话:"+rs
.getString("telephone"));
System
.out
.println("注册时间:"+rs
.getString("register_time"));
}else {
System
.out
.println("查询结果:编号为["+id
+"]的用户不存在!");
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
ConnectionManager
.closeConnection(conn
);
}
}
}
运行程序,查看结果
3.创建Login用户登录类(使用预备语句对象)
package net
.zm
.student
.test
;
import net
.zm
.student
.dbutli
.ConnectionManager
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class Login {
public static void main(String
[] args
) {
String username
;
String password
;
Scanner sc
= new Scanner(System
.in
);
System
.out
.print("输入用户名:");
username
= sc
.next();
System
.out
.print("输入密码:");
password
= sc
.next();
Connection conn
= ConnectionManager
.getConnection();
try {
String strSQL
= "select * from t_user where username = ? and password = ?";
PreparedStatement pstmt
= conn
.prepareStatement(strSQL
);
pstmt
.setString(1, username
);
pstmt
.setString(2, password
);
ResultSet rs
= pstmt
.executeQuery();
if (rs
.next()) {
System
.out
.println("恭喜,登录成功!");
} else {
System
.err
.println("遗憾,登录失败!");
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
ConnectionManager
.closeConnection(conn
);
}
}
}
运行程序,查看结果
4.创建AddUser添加用户记录类
package net
.zm
.student
.test
;
import net
.zm
.student
.bean
.User
;
import net
.zm
.student
.dbutli
.ConnectionManager
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.SQLException
;
import java
.sql
.Timestamp
;
import java
.util
.Date
;
import java
.util
.Scanner
;
public class AddUser {
public static void main(String
[] args
) {
String username
, password
, telephone
;
Scanner sc
= new Scanner(System
.in
);
System
.out
.print("用户名:");
username
= sc
.next();
System
.out
.print("密码:");
password
= sc
.next();
System
.out
.print("电话:");
telephone
= sc
.next();
User user
= new User();
user
.setUsername(username
);
user
.setPassword(password
);
user
.setTelephone(telephone
);
user
.setRegisterTime(new Timestamp(new Date().getTime()));
Connection conn
= ConnectionManager
.getConnection();
String strSQL
= "insert into t_user (username, password, telephone, register_time) values (?, ?, ?, ?)";
try {
PreparedStatement pstmt
= conn
.prepareStatement(strSQL
);
pstmt
.setString(1, user
.getUsername());
pstmt
.setString(2, user
.getPassword());
pstmt
.setString(3, user
.getTelephone());
pstmt
.setTimestamp(4, user
.getRegisterTime());
int count
= pstmt
.executeUpdate();
if (count
> 0) {
System
.out
.println("恭喜,用户记录添加成功!");
} else {
System
.out
.println("遗憾,用户记录添加失败!");
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
ConnectionManager
.closeConnection(conn
);
}
}
}
运行程序,查看结果 注意:这里报错说找不到id字段我去网上查阅资料说,mysql的id字段一定是自增长 这样就不会报错啦!运行DisplayAllUsers程序,查看结果 打开navicat,查看用户表
5.创建UpdateUser更新用户记录类
package net
.zm
.student
.test
;
import net
.zm
.student
.dbutli
.ConnectionManager
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class UpdateUser {
public static void main(String
[] args
) {
int id
;
String username
, password
;
Scanner sc
= new Scanner(System
.in
);
System
.out
.print("待编辑记录的id:");
id
= sc
.nextInt();
System
.out
.print("新用户名:");
username
= sc
.next();
System
.out
.print("新密码:");
password
= sc
.next();
Connection conn
= ConnectionManager
.getConnection();
String strSQL
= "update t_user set username = ?, password = ? where id = ?";
try {
PreparedStatement pstmt
= conn
.prepareStatement(strSQL
);
pstmt
.setString(1, username
);
pstmt
.setString(2, password
);
pstmt
.setInt(3, id
);
int count
= pstmt
.executeUpdate();
if (count
> 0) {
System
.out
.println("恭喜,用户记录更新成功!");
} else {
System
.out
.println("遗憾,用户记录更新失败!");
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
ConnectionManager
.closeConnection(conn
);
}
}
}
运行程序,修改第10条记录的用户名与密码 运行FindUserById程序,查看结果 在NaviCat里查看用户表
6.创建DeleteUserById删除用户记录类
package net
.zm
.student
.test
;
import net
.zm
.student
.dbutli
.ConnectionManager
;
import java
.sql
.Connection
;
import java
.sql
.PreparedStatement
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
public class DeleteUserByld {
public static void main(String
[] args
) {
int id
;
Scanner sc
= new Scanner(System
.in
);
System
.out
.print("待删记录的id:");
id
= sc
.nextInt();
Connection conn
= ConnectionManager
.getConnection();
String strSQL
= "delete from t_user where id = ?";
try {
PreparedStatement pstmt
= conn
.prepareStatement(strSQL
);
pstmt
.setInt(1, id
);
int count
= pstmt
.executeUpdate();
if (count
> 0) {
System
.out
.println("恭喜,用户记录删除成功!");
} else {
System
.out
.println("遗憾,用户记录删除失败!");
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
ConnectionManager
.closeConnection(conn
);
}
}
}
运行程序,查看结果 运行DisplayAllUsers程序,查看结果 可以看到,编号为2的用户记录确实被删除掉了。打开NaviCat,查看用户表