jdbc(工具类和配置文件)
2024-10-21 03:50:04
原始的jdbc要操作7步
- 导入jar包
- 加载驱动
- 获取连接
- 获取执行者对象
- 编写sql语句
- 处理结果
- 释放对象资源
当我们每次都要注册驱动,获取连接的时候,都感觉很烦,这时候怎么才能懒呢?
把driver,url,username,password到配置文件里,就可以一次编写,下次处处使用了!配置文件jdbc.property放在src下的
还要使用工具类,,JDBCUtils工具类是对应普通执行者对象的,JDBCPlusUtils工具类是对应预编译执行者对象的。其中工具类和配置文件的键名和配置文件名都要一致
我们知道SQL注入漏洞是钻了sql语句漏洞所以我们用预编译对象来解决这个问题。
操作步骤
导入jar包和工具类
通过工具类获取连接对象
sql语句的编写
预编译执行者的创建
处理结果
释放资源(这一步我老是忘记!!)
如果是事务操作的话只要通过连接对象开启事务即可,要去判断是否执行成功,成功了就提交,失败要全部回滚
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();
}
}
}
最新文章
- Java实现冒泡排序
- Field 'id' doesn't have a default value(jdbc连接错误)
- jQuery学习笔记(三):选择器总结
- 在网上看到的一篇文章关于js和php编码的
- 乙醇脱氢酶力场文件的处理(含ZN,NAD,乙醇)
- 谁是谁的first-child
- IntelliJ IDEA安装 一些配置
- angular学习(二):Controller定义总结
- [转]解决MySQL出现大量unauthenticated user的问题
- C#Redis 主从复制
- html5移动端meta自动适应标签
- 快递查询 C#
- java之路 数据类型-常量
- Task Schedule HDU - 3572(按时间点建边)
- vue使用element-ui的el-input监听不了回车事件
- tuple的基本使用
- 第五讲 DOM基础
- 【ASP.NET MVC系列】浅谈ASP.NET 程序发布过程
- Web 数据源
- Windows10和CentOS7双系统安装的一些小技巧
热门文章
- DataGrid 设置某列可见或只读
- Nginx03 虚拟主机
- HTML5 + canvas 汽车赛道,飙车游戏(附源码)
- 一文看懂 Python 中的函数参数
- Redis避坑指南:为什么要有分布式锁?
- 构建api gateway之 openresty 中如何使用 wasm
- JZOJ 4211. 【五校联考1day2】送你一颗圣诞树
- MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(3)-系统数据集合设计
- Hugging Face 每周速递: Space 支持创建模版应用、Hub 搜索功能增强、BioGPT-Large 还有更多
- vivado工具ila抓取的波形读取方法