jdbc(工具类和配置文件)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

原始的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();
} } }
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“jdbc(工具类和配置文件)” 的相关文章

Qt每隔N秒运行一个函数

QTimer* mTimer = new QTimer(this); connect(mTimer,SIGNAL(timeout()),this,SLOT(move())); mTimer->start(1000);//From then on, the move() slot is call...

Linux中rz和sz命令用法详解

在linux中rz 和 sz 命令允许开发板与主机通过串口进行传递文件了下面我们就来简单的介绍一下rz 和 sz 命令的例子。 rzsz是Linux/Unix同Windows进行ZModem文件传输的命令行工具。 优点就是不用再开一个sftp工具登录上去上传下载文件。 sz将选定的文件发送se...

【SQL开发实战技巧】系列(三十五):数仓报表场景☞根据条件返回不同列的数据以及Left /Full Join注意事项

系列文章目录 【SQL开发实战技巧】系列一:关于SQL不得不说的那些事 【SQL开发实战技巧】系列二简单单表查询 【SQL开发实战技巧】系列三SQL排序的那些事 【SQL开发实战技巧】系列四从执行计划讨论UNION ALL与空字符串&UNION与OR的使用注意事项 【SQL开发实战技巧】系...

Vim

日常常用到多行合并的功能,记录如下: 第一种, 多行合并成一行,即: AAAAABBBBBCCCCC合并为:AAAAA BBBBB CCCCC 方法1: normal状态下 3J 其中的3是范围,可以是书签或者搜索位置等方式实现,J为合并 注: 如果改为3gJ的话,则合...

linux

via: http://blog.sina.com.cn/s/blog_86969a7e01012e0a.html1. zip 文件乱码解决在windows上压缩的文件,是以系统默认编码中文来压缩文件。由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件...

几类网线是怎么分类的呀?就像3类网线5类网线这些都是按什么分类的?

1)一类线:主要用于传输语音(一类标准主要用于八十年代初之前的电话线缆),不同于数据传输。2)二类线:传输频率为1MHZ,用于语音传输和最高传输速率4Mbps的数据传输,常见于使用4MBPS规范令牌传递协议的旧的令牌网。3)三类线:指目前在ANSI和...