上课视频:
链接:https://pan.baidu.com/s/1-eDmtAwLgt3gnfF4DLe4sQ 密码:0mc8
1.Swing是什么东东?
一.MVC架构思维
二、MVC架构代码
整体代码结构,总的代码分为了
image.png
controler包
控制器包,用于创建什么实类,调用什么管理类在这里控制
db包
(所有的用于直接操作数据库的类都放此包下面)
entity包
实体类包,新有的实体都放到此包下面
manage包
这里把对实的增删改查操作的封装类都下这包下面
MVC的案例
1.控制器类Test
package com.joychenmvc.controler;
import com.joychenmvc.entity.User;
import com.joychenmvc.manager.UserManager;
public class Test {
public static void main(String args[]){
User user = new User(-1,"joychen","123456",1,"0007");
UserManager userManager = new UserManager();
userManager.addUser(user);
}
}
2.用户实体类User
package com.joychenmvc.entity;
public class User {
private int id;
private String username;
private String password;
private int sex;
private String number;
public User() {
}
public User(int id, String username, String password, int sex, String number) {
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
this.number = number;
}
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 int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex=" + sex +
", number='" + number + '\'' +
'}';
}
}
3.用户实体管理类UserManager
package com.joychenmvc.manager;
import com.joychenmvc.db.DBHelper;
import com.joychenmvc.entity.User;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class UserManager {
DBHelper dbHelper;
public UserManager() {
dbHelper = new DBHelper();
}
/**
* @param user
*/
public void addUser(User user) {
String sql = "insert into users(name,password,sex,number) values" +
"('" + user.getUsername() + "','" + user.getPassword() + "'," + user.getSex() +
",'" + user.getNumber() + "')";
dbHelper.init();
dbHelper.updateDb(sql);
dbHelper.colse();
}
public void edidUser(User user) {
String sql = "update users set name='" + user.getUsername() + "',password='" + user.getPassword() + "',sex=" + user.getSex() + ",number='" + user.getNumber() + "' where id=" + user.getId();
dbHelper.init();
dbHelper.updateDb(sql);
dbHelper.colse();
}
public void editUserByName(User user,String name){
String sql = "update users set name='" + user.getUsername() + "',password='" + user.getPassword() + "',sex=" + user.getSex() + ",number='" + user.getNumber() + "' where name=" +name;
dbHelper.init();
dbHelper.updateDb(sql);
dbHelper.colse();
}
public void deleteUser(int id) {
String sql = "delete from users where id=" + id;
dbHelper.init();
dbHelper.updateDb(sql);
dbHelper.colse();
}
public List<User> getAllUsers() {
List<User> users = new ArrayList<>();
String sql = "select * from users";
dbHelper.init();
ResultSet resultSet = dbHelper.query(sql);
try {
while (resultSet.next()) {
int id = resultSet.getInt("id");
String aaa =resultSet.getString("name");
String passwd = resultSet.getString("password");
int sex = resultSet.getInt("sex");
String number =resultSet.getString("number");
User u = new User(id,aaa,passwd,sex,number);
users.add(u);
}
} catch (Exception e) {
}
dbHelper.colse();
return users;
}
}
4.数据库连接类DbHelper
package com.joychenmvc.db;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DBHelper {
//驱动包的路径
String driverName = "com.mysql.jdbc.Driver";
Connection conn;
String username = "root";
String password = "joy123";
String url = "jdbc:mysql://127.0.0.1:3306/studentdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
/**
* 初使化函数
*/
public void init() {
try {
//1.加载驱动类
Class.forName(driverName);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 支持 新增 修改 删除
*
* @param sql
* @return
*/
public int updateDb(String sql) {
int reslut = 0;
try {
Statement statement = conn.createStatement();
reslut = statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return reslut;
}
/**
* 关闭数据库操作
*/
public void colse() {
try {
if (conn != null || !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 执行查询操作
* @param sql
* @return
*/
public ResultSet query(String sql) {
ResultSet resultSet = null;
try {
Statement statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return resultSet;
}
}
网友评论