JDBC

  • 40、Statement对象详解_哔哩哔哩_bilibili

  • SUN公司为了简化开发人员的(对数据库的统一)操作,提供了一个Uva操作数据库的)规范,俗称JDBC

  • 这些规范的实现由具体的厂商去做~

  • 对于开发人员来说,我们只需要掌握JDBC接口的操作即可

image-20230415213941491

依赖

  • java.sql
  • javax.sql
  • 数据库驱动包 mysql-connector-java
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>


尝试Demo

stastement对象写法:

public class JdbcFirstDemo {
public static void main(String[] args) throws SQLException, ClassNotFoundException {

//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");

//2.用户数据和url
String url = "jdbc:mysql://localhost:3306/c2c?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username = "root";
String password = "123456";

//3.执行SQL的对象,连接成功,数据库对象 Connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);

//4。执行SQL的对象 Statement 执行sql的对象
Statement stastement=connection.createStatement();


//5.执行SQL的对象 去执行SQL,可能存在结果、查看返回结果

String sql="select * from admin";//编写SQL

ResultSet resultSet=statement.executeQuery(sql);//返回的结果集,结果集中封装了我们全部的查询出来的结果

while(resultSet.next()){
System.out.println("uid="+resultSet.getString("admid"));
System.out.println("upassword="+resultSet.getString("admpassword"));
System.out.println("umail="+resultSet.getString("admmail"));
}

//释放连接
resultSet.close();
statement.close();
connection.close();
}
}

预编译写法

  • 防止sql注入
public class Sql {
public static void main(String[] args) throws SQLException {

//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");

//2.用户数据和url
String url = "jdbc:mysql://localhost:3306/c2c?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username = "root";
String password = "123456";

//3.执行SQL的对象,连接成功,数据库对象 Connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);

//4.编写$0L
String sql = "insert into users(id,name,password,email,birthday)values (?,?,?,?,?);" ;

//5.预编译
PreparedStatement preparedstatement = connection.prepareStatement(sql);
preparedstatement.setInt(1,1); //给第一个占位符?的值赋值为1;
preparedstatement.setString(2,"辰呀"); //给第二个占位符?的值为辰呀;
preparedstatement.setString(3,"123456"); //给第三个占位符?的值赋值为123456:
preparedstatement.setString(4,"24736743@qq.com"); //给第四个占位符?的值赋值机:
preparedstatement.setDate(5, (java.sql.Date) new Date(new Date().getTime())); //给第五个占位符?的值腻值为new Date

//5.执s0L
int i = preparedstatement.executeUpdate();
if (i>0){
System.out.println("插入成功@");

//释放连接
preparedstatement.close();
connection.close();
}
}

集成封装

  • db.properties

  • driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/smbms?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username=root
    password=123456
    

    <br/>

    ```java
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;

    //操作数据库的公共类
    public class BaseDao {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //静态代码块,类加载的时候就初始化了
    static {
    Properties properties = new Properties();
    //通过类加载器读取对应的资源
    InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");

    try {
    properties.load(is);
    } catch (IOException e) {
    e.printStackTrace();
    }

    driver = properties.getProperty("driver");
    url = properties.getProperty("url");
    username = properties.getProperty("username");
    password = properties.getProperty("password");
    }

    //获取数据库的链接
    public static Connection getConnection(){
    Connection connection = null;
    try {
    Class.forName(driver);
    connection = DriverManager.getConnection(url, username, password);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return connection;
    }

    //编写查询公共方法
    public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
    //预编译的sql,在后面直接执行就可以了
    preparedStatement = connection.prepareStatement(sql);

    for (int i = 0; i < params.length; i++) {
    //setObject,占位符从1开始,但是我们的数组是从0开始!
    preparedStatement.setObject(i+1,params[i]);
    }

    resultSet = preparedStatement.executeQuery();
    return resultSet;
    }


    //编写增删改公共方法
    public static int execute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement) throws SQLException {
    preparedStatement = connection.prepareStatement(sql);

    for (int i = 0; i < params.length; i++) {
    //setObject,占位符从1开始,但是我们的数组是从0开始!
    preparedStatement.setObject(i+1,params[i]);
    }

    int updateRows = preparedStatement.executeUpdate();
    return updateRows;
    }


    //释放资源
    public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
    boolean flag = true;

    if (resultSet!=null){
    try {
    resultSet.close();
    //GC回收
    resultSet = null;
    } catch (SQLException e) {
    e.printStackTrace();
    flag = false;
    }
    }

    if (preparedStatement!=null){
    try {
    preparedStatement.close();
    //GC回收
    preparedStatement = null;
    } catch (SQLException e) {
    e.printStackTrace();
    flag = false;
    }
    }

    if (connection!=null){
    try {
    connection.close();
    //GC回收
    connection = null;
    } catch (SQLException e) {
    e.printStackTrace();
    flag = false;
    }
    }
    return flag;
    }
    }




  • 1、加载驱动
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");


  • 2、连接数据库DriverManager
//2.用户数据和url
String url = "jdbc:mysql://localhost:3306/c2c?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username = "root";
String password = "123456";

//3.执行SQL的对象,连接成功,数据库对象 Connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);


  • connection 代表数据库
  • 开启事务,fasle,开启 connection.setAutoCommit(false);
  • 事务回滚 connection.rollback();
  • 事务提交 connection.commit();


  • 3、获得执行sql的对象Statement
//4。执行SQL的对象 Statement 执行sql的对象
Statement statement=connection.createStatement();


//5.执行SQL的对象 去执行SQL,可能存在结果、查看返回结果

String sql="select * from admin"; //编写SQL
statement.executeQuery();	//查询操作返回结果集ResultSet

statement.execute(); //执行任何SQL

statement.executeUpdate(); //更新、插入、删除、都是用这个,返回一个受影响的行数

statement.executeBatch(); //放入多个SQL同时执行


  • 4、获得返回的结果集ResultSet : 封装了所有的查询结果
//返回的结果集,结果集中封装了我们全部的查询出来的结果
ResultSet resultSet=statement.executeQuery(sql);

类型

​ //在不知道列表类型的情况下使用

  • resultSet.getObject();

    //如果知道列的类型就使用指定的类型

  • resultSet.getString();

  • resultSet.getInt();

  • resultSet.getFloat();

  • resultSet.getDate();

  • resultSet.getObject();

  • 。。。。。



  • 遍历、指针
resultSet.beforeFirst();    //移动到最前面
resultSet.afterLast(); //移动到最后面
resultSet.next(); //移动到下一个数据
resultSet.previous(); //移动到下一行
resultSet.absolute(row); //移动到指定行
while(resultSet.next()){
System.out.println("uid="+resultSet.getString("admid"));
System.out.println("upassword="+resultSet.getString("admpassword"));
System.out.println("umail="+resultSet.getString("admmail"));
}


  • 释放资源
//释放连接
resultSet.close();
statement.close();
connection.close();





statement对象

jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可。

  • Statement.executeUpdate : 该方法用于向数据库发送增、删、改的sql语句,执行完后,将会返回一个整数(数据库几行数据发生变化)
  • Statement.executeQuery : : 该方法用于向数据库发送查询语句,执行完后,将会返回代表查询结果的ResultSet结果集对象




事务

事务ACID理解


  • 原子性

针对同一个事务

原子性表示,这两个步骤一起成功,或者一起失败,不能只发生其中一个动作


  • 一致性(Consistency)

针对一个事务操作前与操作后的状态一致


  • 持久性(Durability)

表示事务结束后的数据不随着外界原因导致数据丢失

事务没有提交,恢复到原样
事务已经提交,持久化到数据库的


  • 隔离性(Isolation)

针对多个用户同时操作,主要是排除其他事务对本次事务的影响

开启事务

  • 开启事务,fasle,开启

connection.setAutoCommit(false);

事务回滚

try{

}catch{
connection.rollback();
}finally{}

事务提交

connection.commit();