JDBC复习

目录

什么是JDBC

图解

需要的jar包

新建一个数据库

导入数据库依赖

JDBC固定步骤

实战1

实战2


什么是JDBC

Java database Connection:Java数据库连接

图解

JDBC复习_第1张图片

需要的jar包

1 java.sql

2 javax.sql

3 mysql-connector-java:连接驱动

新建一个数据库

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `password` varchar(40) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO users (id,name,password,email,birthday)VALUES(1,'张三','123456','[email protected]','2020-01-01');

导入数据库依赖


    
        mysql
        mysql-connector-java
        5.1.46
    

IDEA连接数据源

JDBC复习_第2张图片

JDBC固定步骤

1 加载驱动

2 连接数据库

3 向数据库发送SQL的对象Statement:CRUD

4 编写SQL

5 执行SQL

6 关闭连接

实战1

package com.cakin;


import java.sql.*;


/**
* @ClassName: JdbcTest
* @Description: Jdbc测试
* @Date: 2020/6/18
* @Author: cakin
*/
public class JdbcTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // useUnicode=true&characterEncoding=utf8 解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "123456";


        // 1 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2 获得连接对象
        Connection connection = DriverManager.getConnection(url, username, password);
        // 3 获得发送SQL对象statement,可以进行增删查改操作
        Statement statement = connection.createStatement();
        // 4 编写SQL
        String sql = "select * from users";
        // 5 执行SQL
        ResultSet rs = statement.executeQuery(sql);


        while (rs.next()) {
            System.out.println("id=" + rs.getObject("id"));
            System.out.println("name=" + rs.getObject("name"));
            System.out.println("password=" + rs.getObject("password"));
            System.out.println("email=" + rs.getObject("email"));
        }
        // 6 关闭资源
        rs.close();
        statement.close();
        connection.close();
    }
}

实战2

package com.cakin;


import java.sql.*;


/**
* @ClassName: JdbcTest2
* @Description: jdbc测试
* @Date: 2020/6/18
* @Author: cakin
*/
public class JdbcTest2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // useUnicode=true&characterEncoding=utf8 解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "123456";


        // 1 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2 获得连接对象
        Connection connection = DriverManager.getConnection(url, username, password);
        // 3 编写SQL
        String sql = "insert into users(`id`, `name`, `password`, `email`, `birthday`) values (?,?,?,?,?)";
        // 4 预编译
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 8);
        preparedStatement.setString(2, "cakin");
        preparedStatement.setString(3, "123456");
        preparedStatement.setString(4, "[email protected]");
        preparedStatement.setDate(5, new Date(new java.util.Date().getTime()));
        // 5 执行
        preparedStatement.executeUpdate();


        // 6 关闭资源
        connection.close();
    }
}

 

你可能感兴趣的:(java,java)