zoukankan      html  css  js  c++  java
  • jdbc-mysql链接-基础版

    1、基础连接代码
    public static void main(String[] args) throws SQLException {

    String url = "jdbc:mysql://127.0.0.1:3306/student";
    String userName = "root";
    String password = "11111";
    String sql = "select id,name,age from user;";

    // 1.建立连接
    Connection connection = DriverManager.getConnection(url, userName, password);

    // 2.具体操作
    // 2.1 预编译 SQL
    PreparedStatement statement = connection.prepareStatement(sql);
    // 2.2 执行查询
    ResultSet resultSet = statement.executeQuery();
    // 2.3 处理结果集
    while (resultSet.next()) {
    int id = resultSet.getInt("id");
    String name = resultSet.getString("name");
    int age = resultSet.getInt("age");
    System.out.println("id = " + id + ", name=" + name + ", age=" + age);
    }

    // 3.资源关闭
    resultSet.close();
    statement.close();
    connection.close();
    }

    2、基础连接增删改查-加异常处理
    static String URL = "jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
    static String USER_NAME = "root";
    static String PASSWORD = "123456";

    public static void main(String[] args) {
    // testInsert1();

    testInsert2();

    // testUpdate();

    // insert , update , delete 操作时只有SQL和数据不一样,其他步骤都一样,所以可以归为一类操作
    }

    private static void testInsert1() {
    String sql = "insert into `tb_user`(`name`,`age`) values('jim','66')";

    Connection connection = null;
    PreparedStatement statement = null;

    try {
    // 建立连接
    connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

    // 创建statement 并执行update
    statement = connection.prepareStatement(sql);
    int effectRows = statement.executeUpdate();

    System.out.println("insert effectRows = " + effectRows);

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

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

    private static void testInsert2() {
    String sql = "INSERT INTO `tb_user`(`name`,`age`) VALUES(?,?)";

    Connection connection = null;
    PreparedStatement statement = null;

    try {
    // 建立连接
    connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

    // 创建statement 并执行update
    statement = connection.prepareStatement(sql);
    statement.setString(1, "王五");
    statement.setInt(2, 67);
    int effectRows = statement.executeUpdate();

    System.out.println("insert effectRows = " + effectRows);

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

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

    private static void testUpdate() {
    String sql = "update tb_user set name='haha' where id=6";

    Connection connection = null;
    PreparedStatement statement = null;

    try {
    // 建立连接
    connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

    // 创建statement 并执行update
    statement = connection.prepareStatement(sql);
    int effectRows = statement.executeUpdate();

    System.out.println("update effectRows = " + effectRows);

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

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

    private static void testSelete() {
    String sql = "select id ,name ,age from tb_user;";
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    try {
    // 1.建立连接
    connection = DriverManager.getConnection(url, userName, password);

    // 2.具体操作
    // 2.1 预编译 SQL
    statement = connection.prepareStatement(sql);
    // 2.2 执行查询
    resultSet = statement.executeQuery();
    // 2.3 处理结果集
    while (resultSet.next()) {
    int id = resultSet.getInt("id");
    // 不建议使用
    // int id2 = resultSet.getInt(1);
    // System.out.println("id2="+id2);
    String name = resultSet.getString("name");
    int age = resultSet.getInt("age");
    System.out.println("id = " + id + ", name=" + name + ", age=" + age);
    }

    } catch (SQLException e) {
    e.printStackTrace();// 不推荐的异常处理方式
    } finally {
    // 3.资源关闭
    if (resultSet != null) {
    try {
    resultSet.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

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

    }

    if (connection != null) {
    try {
    connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    }
  • 相关阅读:
    DS博客作业05--查找
    DS博客作业04--图
    DS博客作业03--树
    栈和队列
    第六次作业
    第五次作业
    第四次作业
    第三次作业
    java任务
    第三周-自主学习任务-面向对象基础与类的识别
  • 原文地址:https://www.cnblogs.com/duanjialin007/p/12976033.html
Copyright © 2011-2022 走看看