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();

补充:

  • 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();
}

JDBC基本使用方法的更多相关文章

  1. JDBC连接MySQL 方法 实例及资料收集

    JDBC连接MySQL 方法 实例及资料收集 准备工作 首先,安装MySQL,配置用户名和密码,创建数据库. 可参见之前的文章: http://www.cnblogs.com/mengdd/p/315 ...

  2. 使用JDBC的addBatch()方法提高效率

    在批量更新SQL操作的时候建议使用addBatch,这样效率是高些,数据量越大越能体现出来 Statement接口里有两个方法:void     addBatch(String sql)将给定的 SQ ...

  3. Jmeter之JDBC Request使用方法(oracle)

    JDBC Request: 这个sampler可以向数据库发送一个jdbc请求(sql语句),它经常需要和JDBC Connection Configuration 配置元件一起配合使用. 目录: 一 ...

  4. 加速JDBC的快捷方法

    JAVA 应用必须通过 JDBC 从数据库中取数,有时候我们会发现,数据库的负担一点也不重而且 SQL 很简单,但取数的速度仍然很慢.仔细测试会发现,性能瓶颈主要在 JDBC 上,比如 MySQL 的 ...

  5. JDBC完美连接方法

    jdbc:mysql://localhost:3306:test这句里面分如下解析:jdbc:mysql:// 是指JDBC连接方式:localhost: 是指你的本机地址:3306 SQL数据库的端 ...

  6. 转:JDBC Request使用方法

    1.   下载mysql jar包 下载mysql jar包 http://dev.mysql.com/downloads/connector/j/ 网盘下载地址:mysql-connector-ja ...

  7. JDBC基础-setFetchSize方法

    在Statement和ResultSet接口中都有setFetchSize方法 void setFetchSize(int rows) throws SQLException 查看API文档 Stat ...

  8. jdbc基本查询方法

    jdbc操作数据库时,最基本的三种接口是Statement PreparedStatment  CallableStatement (1)Statement createStatement() cre ...

  9. [数据库操作]Java中的JDBC的使用方法.

    前言:想必大家在实际编码中都遇到过JDBC的操作, 这里仅做自己的一个总结, 有错误和不完整之处还请大家提出来. 1,JDBC其实一套规范(接口)数据库厂商需要实现此接口(实现类)--数据库驱动 2, ...

随机推荐

  1. sqlliab7-8

    less-7 https://www.jianshu.com/p/20d1282e6e1d ?id=0')) union select 1,'2','<?php @eval($_POST[&qu ...

  2. opencv-4-成像系统与Mat图像颜色空间

    opencv-4-成像系统与Mat图像颜色空间 opencvc++qtmat 目标 知道 opencv 处理图像数据的格式 介绍 mat 基础内容 知道 BGR 颜色 显示 颜色转换 BGR 到 灰度 ...

  3. asList和ArrayList不得不说的故事

    目录 简介 创建ArrayList UnsupportedOperationException asList 转换 总结 asList和ArrayList不得不说的故事 简介 提到集合类,ArrayL ...

  4. libcurl库返回状态码解释与速查

    libcurl库返回状态码解释与速查     CURLE_OK(0) 支持返回 CURLE_UNSUPPORTED_PROTOCOL(1) 你的URL传递给libcurl的使用协议,这libcurl的 ...

  5. SpringBoot 集成Swagger2自动生成文档和导出成静态文件

    目录 1. 简介 2. 集成Swagger2 2.1 导入Swagger库 2.2 配置Swagger基本信息 2.3 使用Swagger注解 2.4 文档效果图 3. 常用注解介绍 4. Swagg ...

  6. (转)ATOM介绍和使用

    一,Atom介绍 Atom 是 Github 开源的文本编辑器,这个编辑器完全是使用Web技术构建的(基于Node-Webkit).启动速度快,提供很多常用功能的插件和主题,可以说Atom已经足以胜任 ...

  7. 推荐3个Python初学者学习Python案例

    回复资料,获取最新的Python的资料.想学习Python可以加微信回复报名. 希望今天的分享3个小案例,对你学习Python有帮助 Python 九九乘法表 以下实例演示了如何实现九九乘法表: 实例 ...

  8. js特效:鼠标滑过图片时切换为动图

    效果展示 事前准备 一张普通的静态图+与其对应的gif图. 实现思路 获取图片的src,改变其后缀,使其变成与之对应的gif图片.(很简单有木有= =) 具体实现 编写html代码 <div c ...

  9. 爱创课堂每日一题第五十四天- 列举IE 与其他浏览器不一样的特性?

    IE支持currentStyle,FIrefox使用getComputStyle IE 使用innerText,Firefox使用textContent 滤镜方面:IE:filter:alpha(op ...

  10. STL训练 HDU - 1716 Ray又对数字的列产生了兴趣:

    HDU - 1716 Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(0&l ...