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


补充:
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基本使用方法的更多相关文章
- JDBC连接MySQL 方法 实例及资料收集
JDBC连接MySQL 方法 实例及资料收集 准备工作 首先,安装MySQL,配置用户名和密码,创建数据库. 可参见之前的文章: http://www.cnblogs.com/mengdd/p/315 ...
- 使用JDBC的addBatch()方法提高效率
在批量更新SQL操作的时候建议使用addBatch,这样效率是高些,数据量越大越能体现出来 Statement接口里有两个方法:void addBatch(String sql)将给定的 SQ ...
- Jmeter之JDBC Request使用方法(oracle)
JDBC Request: 这个sampler可以向数据库发送一个jdbc请求(sql语句),它经常需要和JDBC Connection Configuration 配置元件一起配合使用. 目录: 一 ...
- 加速JDBC的快捷方法
JAVA 应用必须通过 JDBC 从数据库中取数,有时候我们会发现,数据库的负担一点也不重而且 SQL 很简单,但取数的速度仍然很慢.仔细测试会发现,性能瓶颈主要在 JDBC 上,比如 MySQL 的 ...
- JDBC完美连接方法
jdbc:mysql://localhost:3306:test这句里面分如下解析:jdbc:mysql:// 是指JDBC连接方式:localhost: 是指你的本机地址:3306 SQL数据库的端 ...
- 转:JDBC Request使用方法
1. 下载mysql jar包 下载mysql jar包 http://dev.mysql.com/downloads/connector/j/ 网盘下载地址:mysql-connector-ja ...
- JDBC基础-setFetchSize方法
在Statement和ResultSet接口中都有setFetchSize方法 void setFetchSize(int rows) throws SQLException 查看API文档 Stat ...
- jdbc基本查询方法
jdbc操作数据库时,最基本的三种接口是Statement PreparedStatment CallableStatement (1)Statement createStatement() cre ...
- [数据库操作]Java中的JDBC的使用方法.
前言:想必大家在实际编码中都遇到过JDBC的操作, 这里仅做自己的一个总结, 有错误和不完整之处还请大家提出来. 1,JDBC其实一套规范(接口)数据库厂商需要实现此接口(实现类)--数据库驱动 2, ...
随机推荐
- PHP扩展Swoole的代码重载机制
大家都知道Swoole的性能在PHP界还算不错,同样都是PHP为什么呢,我专门研究了下. 几个概念: 1) sapi:可以简单的理解为php引擎对外的一个统一接口,使得php可以和外部程序进行交互 ...
- 作业十一——LL(1)文法的判断,递归下降分析程序
作业十一——LL(1)文法的判断,递归下降分析程序 判断是否为LL(1)文法 选取有多个产生式的求select,只有一条产生式的无需求select 同一个非终结符之间求交集,全部判断为空后则为LL(1 ...
- Testing for the End of a File (Windows 的异步 IO)
The ReadFile function checks for the end-of-file condition (EOF) differently for synchronous and asy ...
- fseek 在以字符串模式打开的文件中工作不正常 [MSDN]
For streams opened in text mode, fseek and _fseeki64 have limited use, because carriage return-linef ...
- 面试题总结-Java部分
1 集合 1.1 hashmap原理 HashMap是基于哈希表实现的,每一个元素是一个key-value对,实现了Serializable.Cloneable接口,允许使用null值和null键.不 ...
- dlopen failed: empty/missing DT_HASH in "libx.so" (built with --hash-style=gnu?)
崩溃日志内容: java.lang.UnsatisfiedLinkError: dlopen failed: empty/missing DT_HASH in "libxxxx.so&quo ...
- MySQL数据库的套接字文件和pid文件
MySQL数据库的套接字文件和pid文件 socket文件:当用Unix域套接字方式进行连接时需要的文件. pid文件:MySQL实例的进程ID文件. MySQL表结构文件:用来存放MySQL表结构定 ...
- Swift-Realm数据库的使用详解
Swift-Realm数据库的使用详解 概述 Realm 是一个跨平台的移动数据库引擎,其性能要优于 Core Data 和 FMDB - 移动端数据库性能比较, 我们可以在 Android 端 re ...
- 阿里云ECS安装JAVA+MYSQL+NGINX
2019独角兽企业重金招聘Python工程师标准>>> 1.准备工作 查看linux版本: linux版本为CentOS 7.4 查看系统信息: 系统为64位 确保服务器系统处于最新 ...
- 动态规划经典算法--最长公共子序列 LCS
转移方程 代码: //法一: #include <bits/stdc++.h> using namespace std; //---------------https://lunatic. ...