JDBC整理记录

2020-08-29  本文已影响0人  livesxu

1.什么是JDBC?

Java DataBase Connectivity Java 数据库连接
其实就是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正去执行的代码式驱动jar包的实现类。

2.步骤:

a.导入驱动jar包,需要Add As Library
b.注册驱动对象

mysql注册驱动:mysql5之后的jar包可以省略注册驱动的步骤
Class.forName("com.mysql.cj.jdbc.Driver");

c.定义数据库连接对象 Connection

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbtest","root","bbobbo24");

d.定义sql语句

String sqlString = "delete from tbtest where id = 8;";

e.获取执行sql语句的对象 Statement

Statement statement = connection.createStatement();

f.执行sql,接收返回结果

int count = statement.executeUpdate(sqlString);

g.处理结果

if (count > 0) {
    System.out.println("操作成功");
} else {
    System.out.println("操作失败");
}

h.释放资源

if (connection != null) {
    try {
        connection.close();
     }catch (SQLException e) {
     }
}
if (statement != null) {
    try {
       statement.close();
    } catch (SQLException e) {
    }
}

3.数据库连接对象 Connection

1.获取执行sql的对象Statement
a. createStatement
b. prepareStatement(String sqlString) 可以防止SQL注入,效率更高

String sqlString = "select * from tbtest where worker=? and score > ?;";
preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setInt(1,2);
preparedStatement.setDouble(2,80);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
      System.out.println(resultSet.getInt("id") + " " + resultSet.getString("name"));
}

2.管理事务:

try {
    // 开启事务
    connection.setAutoCommit(false);
    statement = connection.createStatement();
    statement.executeUpdate("update tbtest set score = score + 5 where score > 80;");
    //如果这儿发生了一个错误
    statement.executeUpdate("update tbtest set score = score - 5 where score < 80;");
    //提交事务
    connection.commit();
    System.out.println("成绩更新成功");
 } catch (Exception exception) {
    if (connection != null) {
        try {
        //回滚事务
        connection.rollback();
        System.out.println("回滚事务");
        } catch (Exception exception1) {
        }
    }
    System.out.println(exception.getMessage());
 }

4.Statement 执行sql的对象

5.结果集对象 ResultSet

resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
      System.out.println(resultSet.getInt("id") + " " + resultSet.getString("name"));
}

6数据库连接池

  1. Druid:数据库连接池实现技术,由阿里巴巴提供
    a. 导入jar包 地址
    b. 定义配置文件properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/dbtest
username=root
password=xxxxxx
#初始化连接数量
initialSize=5
#最大连接数
maxActive=20
#最大等待时间
maxWait=3000

c. 加载配置文件
d. 获取数据库连接池对象 DruidDataSourceFactory
e. 获取连接

Properties properties = new Properties();
InputStream inputStream = ConneFour.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(inputStream);
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from tbtest;");
while (resultSet.next()) {
    System.out.println(resultSet.getInt(1) + " " + resultSet.getString(2));
}

7.Spring JDBC

Spring 框架对 JDBC 的封装,提供了一个 JdbcTemplate对象简化JDBC开发
a. 导入jar包 地址

b.创建JdbcTemplate,依赖于数据源DataSource

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

c.执行CRUD

//int infos = jdbcTemplate.update("update tbtest set score=100 where id=1;");
//Map infos = jdbcTemplate.queryForMap("select * from tbtest where id=1;");
//List infos = jdbcTemplate.queryForList("select * from tbtest;");
List<TbInfoBean> infos = jdbcTemplate.query("select * from tbtest;",new BeanPropertyRowMapper<TbInfoBean>(TbInfoBean.class));
上一篇下一篇

猜你喜欢

热点阅读