iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >JDBC基本使用方法
  • 846
分享到

JDBC基本使用方法

JDBC基本使用方法 2017-12-27 07:12:08 846人浏览 才女
摘要

JDBC基本使用方法 JDBC固定步骤: 加载驱动 String url="jdbc:Mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf

JDBC基本使用方法

JDBC基本使用方法

JDBC固定步骤:

加载驱动

String url="jdbc:Mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT";
String username="root";
String passWord="123456";
Class.forName("com.mysql.cj.jdbc.Driver");//这里不知道为什么加载com.mysql.jdbc.Driver会报错,有知道的大佬请留言

连接数据库,代表数据库

Connection connection = DriverManager.getConnection(url, username, password);

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

Statement statement = connection.createStatement();

编写SQL (根据业务, 不同的SQL)

String str1="select * from users";
String str2="insert into users values (4,"赵六","145667","werwef.@eq",current_time) ,(5,"田七","53234","fsd@df",current_time)";
String str3="delete from users where id=5";
String str4="update users set password="987654" where id=4";

执行SQL

//        int i = statement.executeUpdate(str2);
//        int i = statement.executeUpdate(str3);
//        int i = statement.executeUpdate(str4);
        ResultSet resultSet = statement.executeQuery(str1);

遍历结果集


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

关闭连接

resultSet.close();
statement.close();
connection.close();

image-20200519233521651

image-20200519223155014

补充:

  • statement.executeQuery(); //执行查询操作

  • statement.executeUpdate(); //执行增删改操作

  • resultset. beforeFirst(); // 移动到最前面

  • resu1tSet. afterlast(); //移动到最后面

  • resultset.next(); //移动到下一个数据

  • resultset. previous(); //移动到前一行

  • resu1tset. absolute(row); //移动到指定行

  • statement不安全使用prepareStatement 可以防SQL注入

    以下是prepareStatement 的使用方法

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    String url="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT";
    String username="root";
    String password="123456";
    //加载驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    //连接数据库
    Connection connection = DriverManager.getConnection(url, username, password);
    //编写SQL
    String str5="insert into users (id,name,password,email,birthday)values (?,?,?,?,?)";
    //预编译
    PreparedStatement ps = connection.prepareStatement(str5);

    ps.setInt(1,6);				//给第一个占位符?赋值6
    ps.setString(2,"胡八");	   //给第二个占位符?赋值"胡八"
    ps.setString(3,"1223235");	//给第三个占位符?赋值”1223235“
    ps.setString(4,"ew@12");	//给第四个占位符?赋值"ew@12"
    ps.setDate(5,new Date(new java.util.Date().getTime()));
    							//给第五个占位符?赋值2020-05-19
    
    //执行
    int i = ps.executeUpdate();
    if (i>0){
        System.out.println("插入成功");
    }
	//关闭连接
    ps.close();
    connection.close();
}

image-20200519233435322

您可能感兴趣的文档:

--结束END--

本文标题: JDBC基本使用方法

本文链接: https://www.lsjlt.com/news/6172.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql中外码怎么设置
    sql 中外码设置步骤:确定父表和子表。在子表中创建外码列,引用父表主键。使用 foreign key 约束将外码列链接到父表主键。指定引用动作,以处理父表数据更改时的子表数据操作。 ...
    99+
    2024-05-15
  • sql中having是什么
    having 子句用于过滤分组结果,应用于分组后的数据集。它与 where 子句类似,但基于分组结果而不是原始数据。用法:1. 过滤分组后的聚合值。2. 根据分组后的...
    99+
    2024-05-15
  • 在sql中空值用什么表示
    在 sql 中,空值表示未知或不存在的值,可使用 null、空字符串或特殊值表示。处理空值的方法包括使用操作符(is null/is not null)、coalesce 函数(返回第一...
    99+
    2024-05-15
    oracle
  • sql中number什么意思
    sql 中的 number 类型用于存储数值数据,包括小数和整数,特别适合货币、度量和科学数据。其精度由 scale(小数点位数)和 precision(整数字段和小数字段总位数)决定。...
    99+
    2024-05-15
  • sql中空值赋值为0怎么写
    可以通过使用 coalesce() 函数将 sql 中的空值替换为指定值(如 0)。coalesce() 的语法为 coalesce(expression, replacement),其...
    99+
    2024-05-15
  • sql中revoke语句的功能
    revoke 语句用于撤销指定用户或角色的权限或角色成员资格。可撤销的权限包括 select、insert、update、delete 等,撤销的对象类型包括表、视图、存储过程...
    99+
    2024-05-15
    敏感数据
  • sql中REVOKE是什么意思
    revoke 是 sql 中用于撤销用户或角色对数据库对象权限的命令。它通过撤销权限类型、对象级别和目标权限来实现:权限类型:撤销 select、insert、update、d...
    99+
    2024-05-15
  • sql中sp是什么意思
    sql中的sp是存储过程的缩写,它是一种预编译的、已命名的sql语句块,存储在数据库中,可以被用户通过简单命令调用。存储过程的特点有:可重用性、模块化、性能优化、安全性、事务支持。存储过...
    99+
    2024-05-15
    敏感数据
  • sql中references是什么意思
    sql 中的 references 关键字用于在外键约束中定义表之间的父-子关系。外键约束确保子表中的行都引用父表中存在的行,从而维护数据完整性。references 语法的格式为:fo...
    99+
    2024-05-15
  • sql中判断字段为空怎么写
    sql 中可通过 4 种方法判断字段是否为空:1)is null 运算符;2)is not null 运算符;3)coalesce() 函数;4)case 语句。例如,查询所有 colu...
    99+
    2024-05-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作