栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【JDBC】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【JDBC】

Statement

pom.xml



    4.0.0

    org.example
    jdbc
    1.0-SNAPSHOT

    
        8
        8
    

    
        
        
            mysql
            mysql-connector-java
            8.0.22
        

    

package com.xiao.test;

import java.sql.*;

public class Testjdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        //useUnicode=true&characterEncoding=utf-8解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbc?useSSl=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123";

        //1 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2 连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //3 向数据库发送SQL的对象Statement,PreparedStatement : CRUD
        Statement statement = connection.createStatement();
        //4 编写SQL
        String sql = "select * from users";
//        String sql = "delete from users where id = 5";

        //5 执行查询SQL 返回一个 ResultSet结果集
        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"));
            System.out.println("birthday="+rs.getObject("birthday"));
        }
        //6 关闭连接 释放资源(一定要做) 先开后关
        rs.close();
        statement.close();
        connection.close();
    }
}

package com.xiao.test;

import java.sql.*;

public class Testjdbc2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        //useUnicode=true&characterEncoding=utf-8解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbc?useSSl=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123";

        //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 prepareStatement = connection.prepareStatement(sql);

        prepareStatement.setInt(1,5);
        prepareStatement.setString(2,"淼淼");
        prepareStatement.setString(3,"123");
        prepareStatement.setString(4,"miaomiao@163.com");
        prepareStatement.setDate(5,new Date(new java.util.Date().getTime()));//给第五个占位符? 的值赋值为new Date(new java.util.Date().getTime())

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

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

        //6 关闭连接 释放资源(一定要做) 先开后关
        prepareStatement.close();
        connection.close();
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号