文章目录
- 案例1 :修改数据库中的数据
- 案例2:查询数据库中的数据
- 案例3:查询数据库中账户表数据,并将其封装成Account对象,,存储到ArrayList集合当中
- 案例4:商品的增删改查
- 1.准备环境
- 2.查询
- 3.添加
- 4.修改
- 5. 删除
案例1 :修改数据库中的数据
package src.com.itheima.jdbc;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
//jdbc快速入门
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String usename = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, usename, password);
//3.定义sql
String sql = "UPDATE account SET money=1500 WHERE id =2;";
// 4.获取执行sql的对象statement
Statement stmt = conn.createStatement();
//5.执行sql
int count = stmt.executeUpdate(sql); //受影响的行
//6.处理结果
System.out.println(count);
//7.释放资源
stmt.close();
conn.close();
}
}
案例2:查询数据库中的数据
package src.com.itheima.jdbc;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//jdbc快速入门
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String usename = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, usename, password);
//3.定义sql
String sql = "select * from account;";
// 4.获取执行sql的对象statement
Statement stmt = conn.createStatement();
//5.执行sql
//int count = stmt.executeUpdate(sql); //修改
ResultSet resultSet = stmt.executeQuery(sql); //查询
//6.处理结果
while (resultSet.next()){
//获取数据
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
double money = resultSet.getDouble(3);
System.out.println(id);
System.out.println(name);
System.out.println(money);
System.out.println("------------------------");
}
//7.释放资源
resultSet.close();
stmt.close();
conn.close();
}
}
案例3:查询数据库中账户表数据,并将其封装成Account对象,,存储到ArrayList集合当中
2.在java中创建account对象
public class Account {
//这里采用private修饰实现封装,隐藏内部细节防止外部直接访问修改类的私有变量,提高代码的安全型和可维护性;
//如果外部类想访问私有变量,要使用get和set、方法
private int id;
private String name;
private double money;
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 double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
3. 从mysql中读取数据给对象存入ArrayList中并查看
package src.com.itheima.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class SqlAccount {
public static void main(String[] args) throws Exception {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "123456");
//3.定义sql
String sql = "SELECT * FROM account";
//4.获取sql的执行对象
Statement state = conn.createStatement();
//5.执行sql
ResultSet resultSet = state.executeQuery(sql);
//6.处理结果
System.out.println(resultSet); //com.mysql.jdbc.JDBC42ResultSet@6093dd95
System.out.println(resultSet.getClass().getName()); //查看结果类型 :com.mysql.jdbc.JDBC42ResultSet
ArrayList<Account> accounts = new ArrayList<>();
while (resultSet.next()){
//对象创建
Account account = new Account();
//获取数据
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double money = resultSet.getDouble("money");
//给对象赋值
account.setId(id);
account.setName(name);
account.setMoney(money);
//给对象存入集合
accounts.add(account);
}
System.out.println(accounts);
//7.释放资源
resultSet.close();
state.close();
conn.close();
}
}
案例4:商品的增删改查
- 查询:查询所有数据
- 添加:添加品牌·
- 修改:根据id修改
- 删除:根据id删除
1.准备环境
准备品牌表和创建品牌对象
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(
-- id 主键
id int primary key auto_increment,
-- 品牌名称
brand_name varchar(20),
-- 企业名称
company_name varchar(20),
-- 排序字段
ordered int,
-- 描述信息
description varchar(100),
-- 状态:0:禁用 1:启用
status int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
('小米', '小米科技有限公司', 50, 'are you ok', 1);
SELECT * FROM tb_brand;
public class Brand {
Integer id; //这里使用Integer是因为,对应实际的商品如果未负责,使用int默认为0,使用Integer默认是null
String brand_name;
String company_name;
Integer ordered;
String description;
Integer status;
public Brand(Integer id, String brand_name, String company_name, Integer ordered, String description, Integer status) {
this.id = id;
this.brand_name = brand_name;
this.company_name = company_name;
this.ordered = ordered;
this.description = description;
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brand_name='" + brand_name + '\'' +
", company_name='" + company_name + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
2.查询
package src.com.itheima.jdbc.com.brandTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class BrandTest {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "123456");
//定义sql
String sql = "SELECT * FROM tb_brand";
//执行sql
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
//处理结果
ArrayList<Brand> brands = new ArrayList<>();
while (rs.next()){
int id = rs.getInt("id");
String brand_name = rs.getString("brand_name");
String company_name = rs.getString("company_name");
int ordered = rs.getInt("ordered");
String description = rs.getString("description");
int status = rs.getInt("status");
Brand brand = new Brand(id,brand_name,company_name,ordered,description,status);
brands.add(brand);
}
System.out.println(brands);
//释放资源
rs.close();
stat.close();
conn.close();
}
}
3.添加
package src.com.itheima.jdbc.com.brandTest;
import java.sql.*;
import java.util.ArrayList;
public class BrandTest {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "123456");
//接受页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘";
int ordered = 1;
String description = "绕地球一圈";
int status = 1;
//定义sql
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
//获取ppst
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
//执行sql
int i = pstmt.executeUpdate();
//处理结果
System.out.print("受影响的行数");
System.out.println(i);
//释放资源
pstmt.close();
conn.close();
}
}
4.修改
package src.com.itheima.jdbc.com.brandTest;
import java.sql.*;
import java.util.ArrayList;
public class BrandTest {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "123456");
//接受页面提交的参数
String companyName = "香飘飘科技有限公司啦啦啦";
//定义sql
String sql = "UPDATE tb_brand SET company_name= ? WHERE id = 5;";
//获取ppst
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置参数
pstmt.setString(1,companyName);
//执行sql
int i = pstmt.executeUpdate();
//处理结果
System.out.print("受影响的行数");
System.out.println(i);
//释放资源
pstmt.close();
conn.close();
}
}
5. 删除
package src.com.itheima.jdbc.com.brandTest;
import java.sql.*;
import java.util.ArrayList;
public class BrandTest {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "123456");
//定义sql
String sql = " DELETE FROM tb_brand WHERE id = 1;";
//执行sql
Statement stst = conn.createStatement();
int i = stst.executeUpdate(sql);
//处理结果
System.out.print("受影响的行数");
System.out.println(i);
//释放资源
stst.close();
conn.close();
}
}