19-JDBC复习

19-JDBC复习

概述

JDBC是Java DataBase Connectivity的缩写,顾名思义:java数据库连接器,负责完成java连接数据库的操作

重点

使用步骤

  1. 加载驱动
  2. 创建连接
  3. 创建像数据库发送sql的Statement对象
  4. 关闭Statement对象
  5. 关闭数据库连接

实践

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>JavaWeb-Study</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>10-jdbc</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>

</project>

Jdbc1.java

package com.kuang.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 功能描述
 *
 * @since 2022-08-31
 */
public class Jdbc1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // useUnicode=true&CharactorEncoding=utf-8
        String url = "jdbc:mysql://xxx:3306/xxx";
        String user = "xxx";
        String password = "xxx";

        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 连接数据库
        Connection connection = DriverManager.getConnection(url, user, password);

        // 向数据库发送sql的对象Statement
        Statement statement = connection.createStatement();
        String sql = "select * from user";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            System.out.println("id="+resultSet.getObject("id"));
            System.out.println("name="+resultSet.getObject("name"));
            System.out.println("pwd="+resultSet.getObject("pwd"));
        }
        // 关闭资源池
        statement.close();
        connection.close();

    }
}

Jdbc2.java

package com.kuang.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 功能描述
 *
 * @since 2022-08-31
 */
public class Jdbc2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // useUnicode=true&CharactorEncoding=utf-8
        String url = "jdbc:mysql://xxx:3306/xxx";
        String user = "xxx";
        String password = "xxx";

        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 连接数据库
        Connection connection = DriverManager.getConnection(url, user, password);
        // 向数据库发送sql的对象 Statement
        String sql = "insert into user (is,name,pwd) values (?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1,10);
        preparedStatement.setObject(2,"lala");
        preparedStatement.setObject(3,"1222");

        int i = preparedStatement.executeUpdate();
        if (i>0){
            System.out.println("插入成功");
        }

        // 关闭资源(一定要做),先开后关
        preparedStatement.close();
        connection.close();
    }
}
posted @ 2022-09-02 19:16  Oh,mydream!  阅读(11)  评论(0编辑  收藏  举报