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, ...
随机推荐
- 当git上只做文件大小写重命名的修改时,如何躲坑
一. 提交时 假设修改ABC.java为Abc.java. 1.1 如果使用git命令进行仅涉及大小写的重命名 1.1.1 设置git库为大小写敏感(不建议) $ git config core.ig ...
- while循环脚本
[root@oldboy ~]# (while :;do date;sleep 5;done)& fg ctrl c退出 fg ( while :; do date; sleep 5; don ...
- 使用nodejs + wecharty打造你的个人微信机器人
开源地址:https://github.com/isnl/wechat-robot 注: 从2017年6月下旬开始,使用基于web版微信接入方案存在大概率的被限制登陆的可能性. 主要表现为:无法登陆W ...
- 在线图片资源转换成Base64格式
function getBase64Image(img) { var canvas = document.createElement("canvas"); canvas.width ...
- spring IoC容器类接口关系梳理
整理了下spring中容器类的接口,用UML画了张图(并不十分严格按照UML标准,省略了些方法).
- Hawkeye部署Github监控系统
2019独角兽企业重金招聘Python工程师标准>>> step1:python环境安装 #pwd /usr/local/soft #wget https://www.python. ...
- centos6 yum安装jdk1.8+
一.环境Linux操作系统: centos6.9 安装jdk版本: jdk1.8+ 二.安装步骤1. 检查系统是否自带有jdk[root@VM_0_11_centos ~]# rpm -qa |gre ...
- 网络流--最大流--POJ 1459 Power Network
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...
- 题目分享F 二代目
题意:T个点R种双向边,P种单向边,求点S到每个点的最短距离 分析:(这再看不出来是spfa就该**了) 首先,这题能否用spfa就看他是否有负环呗,显然,双向边的权值非负,单向边还有个啥政策,总之显 ...
- B - Lawrence HDU - 2829 斜率dp dp转移方程不好写
B - Lawrence HDU - 2829 这个题目我觉得很难,难在这个dp方程不会写. 看了网上的题解,看了很久才理解这个dp转移方程 dp[i][j] 表示前面1~j 位并且以 j 结尾分成了 ...