文章目录
1.前提准备:
1.1创建数据库
use day04_db;
create table user (
id int primary key auto_increment,
username varchar(20) not null,
birthday date,
sex char(1) default '男',
address varchar(50)
);
insert into user values (null, '孙悟空','1980-10-24','男','花果山水帘洞');
insert into user values (null, '白骨精','1992-11-12','女','白虎岭白骨洞');
insert into user values (null, '猪八戒','1983-05-20','男','福临山云栈洞');
insert into user values (null, '蜘蛛精','1995-03-22','女','盘丝洞');
select * from user;
1.2创建实体类对象
package cn.itcast.domain;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
1.3 JDBC工具类
jdbc。properties文件
url=jdbc:mysql://localhost/day04_db
username=root
password=root
driverClass=com.mysql.jdbc.Driver
package cn.itcast.utils;
import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
//思路:方便对数据库的使用,省去了大量的创建连接数据库使用语句
//基本步骤
//1.对基本的语句抽取成静态代码块
//2.创建连接的方法
//创建销毁的方法
private static String driverClass;
private static String url;
private static String username;
private static String password;
static {
try {
Properties properties= new Properties();
properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
username= properties.getProperty("username");
password= properties.getProperty("password");
Class.forName(driverClass);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void release(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.基于jdbc工具类将数据封装到实体类中
package cn.itcast.test;
import cn.itcast.domain.User;
import cn.itcast.utils.JDBCUtils;
import org.omg.CORBA.UserException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JDBCText03 {
public static void main(String[] args) throws Exception {
List<User> list = new ArrayList<User>();
Connection connection = JDBCUtils.getConnection();
String sql ="select * from user";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Integer id = resultSet.getInt("id");
String username = resultSet.getString("username");
Date birthday = resultSet.getDate("birthday");
String sex = resultSet.getString("sex");
String address = resultSet.getString("address");
User user = new User();
user.setId(id);
user.setUsername(username);
user.setBirthday(birthday);
user.setSex(sex);
user.setAddress(address);
list.add(user);
}
for (User user : list) {
System.out.println(user);
}
JDBCUtils.release(connection,preparedStatement,resultSet);
}
}
查询结果
3.c3p0连接池的使用
使用连接池的好处:面试题
优化了连接,省去了创建连接与销毁链接的时间,提高了增删改查的效率、
3.1c3p0的使用步骤
c3p0连接池的使用步骤
1.创建项目
2.创建lib文件夹,导入c3p0依赖包 add as libery加载到项目中
创建c3p0核心配置文件,连接数据库
创建jdbc.properties文件保存四大内置对象
编写测试类
创建连接池的好处:省去了创建连接归还链接所用的时间,大大提升了效率
优化了创建连接所用的时间
3.2创建项目导包
还有MySQL的驱动包
3.3创建核心配置文件
在src目录下创建
<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/day04_db</property>
<property name="user">root</property>
<property name="password">root</property>
</default-config>
<named-config name="intergalactoApp">
</named-config>
</c3p0-config>
3.4创建测试类
package cn.itcast.web;
import cn.itcast.utils.JDBCUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Demo01 {
public static void main(String[] args) throws Exception {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
Connection connection = comboPooledDataSource.getConnection();
String sql = "select * from user";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String id = resultSet.getString("id");
String username = resultSet.getString("username");
String birthday = resultSet.getString("birthday");
String sex = resultSet.getString("sex");
String address = resultSet.getString("address");
System.out.println(id + username + birthday + sex + address);
}
JDBCUtils.release(connection,preparedStatement,resultSet);
}
}
3.5根据id查询
@Test
public void test01() throws Exception {
//根据id查询
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
Connection connection = comboPooledDataSource.getConnection();
String sql = "select * from user where id=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 1);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String id = resultSet.getString("id");
String username = resultSet.getString("username");
String birthday = resultSet.getString("birthday");
String sex = resultSet.getString("sex");
String address = resultSet.getString("address");
System.out.println(id + username + birthday + sex + address);
}
JDBCUtils.release(connection,preparedStatement,resultSet);
}
3.6添加数据
@Test
public void test02() throws Exception {
//添加
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
Connection connection = comboPooledDataSource.getConnection();
String sql="insert into user values(null,?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "王五");
preparedStatement.setString(2, "2020-01-01");
preparedStatement.setString(3, "男");
preparedStatement.setString(4, "广州");
int i = preparedStatement.executeUpdate();
if (i == 1) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
JDBCUtils.release(connection,preparedStatement,null);
}
3.7修改数据
@Test
public void test03() throws Exception {
//修改
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
Connection connection = comboPooledDataSource.getConnection();
String sql = "update user set username=? ,birthday=? ,sex=? where id=? ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "全威");
preparedStatement.setString(2, "2020-01-01");
preparedStatement.setString(3, "男");
preparedStatement.setInt(4, 11);
int i = preparedStatement.executeUpdate();
if (i == 1) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
JDBCUtils.release(connection, preparedStatement, null);
}
3.8删除数据
@Test
public void test05() throws Exception {
//删除
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
Connection connection = comboPooledDataSource.getConnection();
String sql = "delete from user where id=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 12);
int i = preparedStatement.executeUpdate();
if (i == 1) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
JDBCUtils.release(connection, preparedStatement, null);
}