1、封装
javabean/实体类
Persion{
属性
方法
}
让属性不可以直接获取,得到方法(get/set)得到
封装的时候针对的是表,所以我们的实体类的所有的属性都要和表中的字段对应
回忆(*****):
集合:存储各种数据类型。
List常用,存放对象add();集合的对象读
结果集存储
List
while (resultSet.next()){
// System.out.println(resultSet.getObject("id"));
// System.out.println(resultSet.getObject("username"));
// System.out.println(resultSet.getObject("password"));
// System.out.println(resultSet.getObject("nickname"));
user user = new user();
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setNickname(resultSet.getString("nickname"));
list.add(user);
}
结果集读取
//循环打印
for (user u:list){
System.out.println(u.toString());
}
//迭代方式打印
Iterator
while (iterator.hasNext()){
user next = iterator.next();
System.out.println(next);
}
代码 user_ bean;
package com.ww.jdbc_demo_01.javabean;
public class user {
//将所有的属性私有化
private int id ;
private String username;
private String password;
private String nickname;
//为了产生实体类对象,构造方法必须有无参和全参
public user() {
}
public user(int id, String username, String password, String nickname) {
this.id = id;
this.username = username;
this.password = password;
this.nickname = nickname;
}
//想要存储属性的值,通过get/set方法
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 getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
//@Override注解
@Override
public String toString() {
return "user{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
1. 操作数据库(CRUD)增删改查
1.1 、单元测试 (属于第三方工具 所以得导入jar包)
JUnit 导入依赖
在测试添加@Test注解
导入依赖
代码展示:
//可执行标记相当于main
// 一个类可以有多个执行接口
// @Test
{
System.out.println("单元测试");
}
// @Test
public void select(){
}
1.2要想完成增删改查 CRUD
步骤:
加载驱动
获取连接
创建SQL执行对象(CRUD+)
执行SQL
释放资源
//增加,修改,删除
//增加测试
// @Test
public void insertuser() throws ClassNotFoundException, SQLException {
//加载驱动
String insert_sql = "insert into user values(null,'zhangge','z123546','张三')";
String url ="jdbc:mysql://localhost:3306/ooo";
String username = "root";
String password = "123456";
//创建连接
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
statement.executeUpdate(insert_sql);
statement.close();
connection.close();
}
public void updateuser() throws ClassNotFoundException, SQLException {
String update_sql= "update user set nickname = '李素' where id = 4 ";
String url ="jdbc:mysql://localhost:3306/ooo";
String username = "root";
String password = "123456";
//创建连接
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
statement.executeUpdate(update_sql);
statement.close();
connection.close();
}
public void deleteuser() throws ClassNotFoundException, SQLException {
String delete_sql = "delete form user where id = 4 ";
String url = "jdbc:mysql://localhost:3306/ooo";
String username = "root";
String password = "123456";
//创建连接
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
statement.executeUpdate(delete_sql);
statement.close();
connection.close();
}