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. Windows挂载Gluster复制卷

    本地挂载测试 mount -t glusterfs 127.0.0.1:/gv1 /mnt [root@gluster1 mnt]# df -h Filesystem Size Used Avail ...

  2. sed命令的正则表达式实践

    1. 取系统ip [root@oldboy logs]# ifconfig eth3 eth3 Link encap:Ethernet HWaddr 08:00:27:4C:6F:AD inet ad ...

  3. React技术栈——Redux

    Redux 1.Redux是什么?   Redux对于JavaScript应用而言是一个可预测状态的容器.换言之,它是一个应用数据流框架,而不是传统的像underscore.js或者AngularJs ...

  4. Hawkeye部署Github监控系统

    2019独角兽企业重金招聘Python工程师标准>>> step1:python环境安装 #pwd /usr/local/soft #wget https://www.python. ...

  5. 如何在linux服务器下快速安装配置Node.js

    简单粗暴,先用xshell或其他软件连接服务器 1.下载(此处版本根据官网版本自己修改) wget https://npm.taobao.org/mirrors/node/v8.9.3/node-v8 ...

  6. stl的stack在开发中的应用

    栈有后进先出特点,我们可以用它来暂时保存数据,在画板开发中,我用到了栈来保存用户的每一步操作,当用户点击撤销时可以把图像从栈里面取出,然后恢复.浏览器的前进和后退也是这个原理,只是它保存的是网页罢了. ...

  7. Mysql 开窗函数实战

    Mysql 开窗函数实战 Mysql 开窗函数在Mysql8.0+ 中可以得以使用,实在且好用. row number() over rank() over dense rank() ntile() ...

  8. codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)

    ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...

  9. UTC 时间转化为北京时间

    // UTC 时间转化为北京时间 function utc2beijing(utcTime) { var T_pos = utcTime.indexOf('T'); var Z_pos = utcTi ...

  10. P1640 连续攻击游戏

    题目传送门 Ⅰ.二分图匹配 其实这题应该不难看出是二分图匹配(尽管我没看出来) 每个物品只能用一次,实际上就是1~n的数字对物品的最大匹配 把物品的两个属性向物品编号连边,之后就从数字1一直匹配过去 ...