学习博客:【JavaWeb】JDBC

Jar包支持

  • java.sql
  • javax.sql
  • mysql-connector-java

编写sql

CREATE TABLE `users`(
    `id` INT(2) PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(5),
    `password` VARCHAR(10),
    `email` VARCHAR(20),
    `birthday` DATE
)ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO `users`(id,`name`,`password`,`email`,`birthday`)
VALUES(1,"杨树","123456","1111@qq.com","1989-12-21");

INSERT INTO `users`(id,`name`,`password`,`email`,`birthday`)
VALUES(3,"杨非","121456","1112221@qq.com","1999-12-21");

导入数据库依赖

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
    </dependencies>

连接步骤

  1. 加载驱动
  2. 连接数据库
  3. 向数据库发送SQL对象Statement / PreparedStatement
  4. 编写SQL
  5. 执行SQL
  6. 关闭连接

不安全连接

package com.yl.TestJdbc;

import java.sql.*;

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

        //配置信息
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=true";
        String username = "root";
        String password = "yL@98";

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

        //2.连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);

        //3.向数据库发送对应statement
        Statement statement = connection.createStatement();

        //4.编写SQL
        String sql = "select * from users";

        //5.执行SQL 返回结果集 ResultSet
        ResultSet rs = statement.executeQuery(sql);

        while(rs.next()){
            System.out.println("id:" + rs.getInt("id"));
            System.out.println("name:" + rs.getString("name"));
            System.out.println("password:" + rs.getString("password"));
            System.out.println("email:" + rs.getString("email"));
            System.out.println("birthday:" + rs.getDate("birthday"));
        }

        //6.释放资源 先开后关
        rs.close();
        statement.close();
        connection.close();
    }
}

安全连接

package com.yl.TestJdbc;

import java.sql.*;

public class TestJdbc2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=true";
        String username = "root";
        String password = "yL@98";

        //1.加载驱动
        Class.forName("com.mysql.cj.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, 2);
        preparedStatement.setString(2, "杨洋");
        preparedStatement.setString(3, "12221");
        preparedStatement.setString(4, "41414141@qq.com");
        preparedStatement.setDate(5, new Date(System.currentTimeMillis()));

        //5.执行SQL
        int i = preparedStatement.executeUpdate();

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

        //6.释放资源
        preparedStatement.close();
        connection.close();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值