回顾:
API
DriverManager:加载驱动、创建连接
Class.forName()
Connection:创建SQL执行对象、创建SQL预编译执行对象
Statement:执行查询语句;执行增加、删除、修改语句;任何SQL
ResultSet:结果集的处理。next() getObject(1) getInt(“id”) getString() getDate()……..
1、封装
javabean / 实体类
Person{
属性
方法
}
让属性不能直接获取,得通过方法(get/set)得到
封装的时候针对的是表,所有我们的实体类的所有的属性都要和表中的字段对应。
回忆(******):
集合:存放各种数据类型。
List常用,存放对象用add();集合的对象的读
代码:
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
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
2、结果集存放
List<User> list=new ArrayList<User>();//集合存放的是User对象
while(resultSet.next()){
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);
}
3、结果集读取
//迭代方式
Iterator iterrator=list.iterator();
while(iterrator.hasNext()){
User user=(User)iterrator.next();
System.out.println(user.toString());
}
//增强for循环
for(User u:list){
System.out.println(u.toString());
}
4、操作数据库(CRUD)
4.1、单元测试
JUnit 导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
代码:
package com.ww.jdbc.junit;
import org.junit.Test;
public class JdbcDemo01 {
@Test
public void selectSql(){
System.out.println("单元测试");
}
@Test
public void selectUpdate(){
}
}
4.2、CRUD
步骤:
加载驱动
获取连接
创建SQL执行对象(CRUD)
执行SQL
释放资源
//增加、修改、删除
package com.ww.jdbc.junit;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo02 {
//增加
@Test
public void insertUser() throws ClassNotFoundException, SQLException {
String insert_sql="insert into user values(null,'lisi','lisi12345','李四')";
String url="jdbc:mysql://localhost:3306/db02";
String username="root";
String password="Root123456";
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();
}
//修改
@Test
public void updateUser() throws ClassNotFoundException, SQLException {
String updata_sql="update user set nickname='李五' where id=4";
String url="jdbc:mysql://localhost:3306/db02";
String username="root";
String password="Root123456";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection= DriverManager.getConnection(url,username,password);
Statement statement=connection.createStatement();
statement.executeUpdate(updata_sql);
statement.close();
connection.close();
}
//删除
@Test
public void deleteUser() throws ClassNotFoundException, SQLException {
String delete_sql="delete from user where id=4";
String url="jdbc:mysql://localhost:3306/db02";
String username="root";
String password="Root123456";
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();
}
}