原始的jdbc要操作7步

  1. 导入jar包
  2. 加载驱动
  3. 获取连接
  4. 获取执行者对象
  5. 编写sql语句
  6. 处理结果
  7. 释放对象资源

当我们每次都要注册驱动,获取连接的时候,都感觉很烦,这时候怎么才能懒呢?

把driver,url,username,password到配置文件里,就可以一次编写,下次处处使用了!配置文件jdbc.property放在src下的

还要使用工具类,,JDBCUtils工具类是对应普通执行者对象的,JDBCPlusUtils工具类是对应预编译执行者对象的。其中工具类和配置文件的键名和配置文件名都要一致

我们知道SQL注入漏洞是钻了sql语句漏洞所以我们用预编译对象来解决这个问题。

操作步骤

  1. 导入jar包和工具类

  2. 通过工具类获取连接对象

  3. sql语句的编写

  4. 预编译执行者的创建

  5. 处理结果

  6. 释放资源(这一步我老是忘记!!)


如果是事务操作的话只要通过连接对象开启事务即可,要去判断是否执行成功,成功了就提交,失败要全部回滚

ResultSet注意事项

数据库查询好数据后会一般会返回两种类型,其中一种是结果集

就算没有查询到数据他也会返回对象,只是这个对象里面没有值而已,所以判断时不可以用resultset!=null去判断

要用resultset.next()去判断true or false

jdbc.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/stuexpm?serverTimezone=GMT
username=root
password=root

相关代码

package com.tbb.test;

import com.sun.security.jgss.GSSUtil;
import com.tbb.lib.utils.JDBCPlusUtils;
import com.tbb.lib.utils.JDBCUtils; import java.sql.*; public class Test2 {
public static void main(String[] args) throws SQLException {
String username="gjj";
String password="123 or 1=1";
preLogin(username,password);
}
public static void preLogin(String name,String pwd) throws SQLException {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
connection = JDBCPlusUtils.getConnection();
String sql="select * from user where username = ? and password = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,pwd);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
} }
public static void statementLogin(String username,String password) throws SQLException {
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
connection = JDBCUtils.getConnection();
statement = connection.createStatement();
String sql="select * from user where username = '"+username+"' and password = '"+password+"';";
resultSet = statement.executeQuery(sql);
System.out.println(resultSet);
if(resultSet!=null){
System.out.println("登陆成功");
}else{
System.out.println("用户名或密码错误");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
connection.close();
statement.close();
resultSet.close();
}
}

我们知道Connection对象可以创建执行者对象,可以释放资源,可以操作事物

package com.tbb.test;

import com.tbb.lib.utils.JDBCPlusUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException; public class Test3 {
public static void main(String[] args) {
transaction();
}
public static void transaction(){
Connection connection = JDBCPlusUtils.getConnection();
try {
connection.setAutoCommit(false);
String sql1="update goods set unitprice=unitprice+1000 where goodsid=3001";
String sql2="update goods set unitprice=unitprice-1000 where goodsid=4001";
PreparedStatement preparedStatement = connection.prepareStatement(sql1);
PreparedStatement preparedStatement1 = connection.prepareStatement(sql2);
int i = preparedStatement.executeUpdate();
int i1 = preparedStatement.executeUpdate();
System.out.println(i);
System.out.println(i1);
if(i!=0 && i1!=0){
System.out.println("执行成功"); }else{
System.out.println("执行失败");
}
preparedStatement.close();
connection.close(); } catch (SQLException throwables) {
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
throwables.printStackTrace();
} } }

最新文章

  1. Java实现冒泡排序
  2. Field 'id' doesn't have a default value(jdbc连接错误)
  3. jQuery学习笔记(三):选择器总结
  4. 在网上看到的一篇文章关于js和php编码的
  5. 乙醇脱氢酶力场文件的处理(含ZN,NAD,乙醇)
  6. 谁是谁的first-child
  7. IntelliJ IDEA安装 一些配置
  8. angular学习(二):Controller定义总结
  9. [转]解决MySQL出现大量unauthenticated user的问题
  10. C#Redis 主从复制
  11. html5移动端meta自动适应标签
  12. 快递查询 C#
  13. java之路 数据类型-常量
  14. Task Schedule HDU - 3572(按时间点建边)
  15. vue使用element-ui的el-input监听不了回车事件
  16. tuple的基本使用
  17. 第五讲 DOM基础
  18. 【ASP.NET MVC系列】浅谈ASP.NET 程序发布过程
  19. Web 数据源
  20. Windows10和CentOS7双系统安装的一些小技巧

热门文章

  1. DataGrid 设置某列可见或只读
  2. Nginx03 虚拟主机
  3. HTML5 + canvas 汽车赛道,飙车游戏(附源码)
  4. 一文看懂 Python 中的函数参数
  5. Redis避坑指南:为什么要有分布式锁?
  6. 构建api gateway之 openresty 中如何使用 wasm
  7. JZOJ 4211. 【五校联考1day2】送你一颗圣诞树
  8. MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(3)-系统数据集合设计
  9. Hugging Face 每周速递: Space 支持创建模版应用、Hub 搜索功能增强、BioGPT-Large 还有更多
  10. vivado工具ila抓取的波形读取方法