当前位置:网站首页>JDBC中的增删改查操作
JDBC中的增删改查操作
2022-08-09 09:34:00 【Living_Amethyst】
JDBC中的增删改查操作
升级版插入操作
上一篇文章里插入的代码,其实我们是把插入操作写“死”了
String sql = "insert into student values(1,'张三')";
PreparedStatement statement = connection.prepareStatement(sql);
也就是插入的只能是张三,但很多时候我们是希望用户自己来插入自己想要插入的内容和数据,于是就有了下面一种方法
//可以由用户自己插入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
int num = scanner.nextInt();
System.out.println("请输入姓名:");
String name = scanner.next();
String sql = "insert into student values(" + num + ", '" + name + "')";
PreparedStatement statement = connection.prepareStatement(sql);
图中框起来的是两个完整的字符串
我们运行一下

再检验一下是否操作成功了

虽然通过拼接字符串的方式,可以完成这里的插入数据功能,但是不太好
主要是因为
这样字符串拼接,代码比较丑陋,也容易拼错,尤其是少个 ’ (单引号) 之类…
这样写,也存在一定的安全性风险,SQL注入攻击
SQL注入攻击 是一种比较经典的攻击方式,
name是用户自己构造的,如果用户故意的构造一-些其他特殊的数据,可能导致出现严重后果!!
如:
name = '); drop tal......
那么究竟如何写才是比较科学的呢?
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
int num = scanner.nextInt();
System.out.println("请输入姓名:");
String name = scanner.next();
//科学的写法
//使用 ? 作为占位符,后续使用 statement对象针对 ? 进行替换。
String sql = "insert into student values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,num);
statement.setString(2,name);
System.out.println(statement);//查看刚刚构造的SQL
我们解析一下这个代码
先使用 ? 作为占位符,后续使用 statement对象针对 ? 进行替换。
再使用statement对象针对==?== 替换
这个操作的意思就是把第一个问号 用 num 替换,num从1开始计数,不是从0开始!
类似的,替换第二个问号
我们运行一下程序

再查看下数据库

证明刚刚的操作是成功的
实现数据库的修改操作
修改操作和上面的插入操作非常类似,只是这里构造的 SQL 是 update 语句
代码:
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCUpdate {
public static void main(String[] args) throws SQLException {
//实现数据库的修改操作,修改操作和上面的插入操作非常类似,只是这里构造的 SQL 是 update 语句
//1.构造数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("123456");
//2.和数据库建立连接
Connection connection = dataSource.getConnection();
//3.用户输入 要修改的 id 和 修改后的名字
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要修改的同学的学号:");
int id = scanner.nextInt();
System.out.println("你要把该同学的姓名修改为:");
String name = scanner.next();
//4.构造 SQL 语句
String sql = "update student set name = ? where id = ? ";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,id);
System.out.println(statement);
//5. 执行SQL
int n = statement.executeUpdate();
System.out.println("n= "+n);
//6.关闭连接,释放资源
statement.close();
connection.close();
}
}
可以看出,与上面的插入操作步骤几乎一样
删除数据库中的数据操作
代码:
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCDelete {
public static void main(String[] args) throws SQLException {
//实现删除操作
//1.构造数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("123456");
//2.和数据库建立连接
Connection connection = dataSource.getConnection();
//3.输入要删除的内容
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生的id:");
int id = scanner.nextInt();
//4.构造SQL
String sql = "delete from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
//5.执行SQL
int n = statement.executeUpdate();
System.out.println("n = "+n);
//6.关闭连接,释放资源
statement.close();
connection.close();
}
}
删除成功
查找操作
查找操作和前面的操作就不太一样了,多了一个步骤,要遍历结果集合
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCSelect {
public static void main(String[] args) throws SQLException {
//查找操作
//1.构造数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("123456");
//2.和数据库建立连接
Connection connection = dataSource.getConnection();
//3.(此处不输入条件了,直接查找所有记录)
//4.构造sql
String sql = "select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
//5.执行SQL,此处要使用的是executeQuery(),
// 因为executeUpdate()只能返回一个int,不符合查找的特点
// 而executeQuery()返回的是一个 ResultSet对象,可以把这个对象视为一个“临时表”
ResultSet resultSet = statement.executeQuery();
//6.遍历临时表,拿到里面的数据
//resultSet可以简单的理解成一个类似于“迭代器”这样的东西
//这里的 next 如果没有到达末尾 返回true,要继续循环,否则返回 false结束循环
while(resultSet.next()){
//这里就可以取这一行的数据了
//通过resultSet里面的 getXXX 方法,来获取到这里指定的列
//取id,参数是列名
int id = resultSet.getInt("id");
//取name
String name = resultSet.getString("name");
//打印
System.out.println(id+":" + name);
}
//7.关闭连接,释放资源
resultSet.close();
statement.close();
connection.close();
}
}
边栏推荐
猜你喜欢
Arrays类、冒泡排序、选择排序、插入排序、稀疏数组!
nacos从下载到安装集群的
有返回值的函数
软件测试流程包括哪些内容?测试方法有哪些?
A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)
游戏测试的概念是什么?测试方法和流程有哪些?
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
.equals ==
列表
Ontology Development Diary 05-Strive to Understand SWRL (Part 2)
随机推荐
Firebase+Facebook 授权 web 登录提示域名验证或跳转错误
接口测试的概念、目的、流程、测试方法有哪些?
LeetCode56:合并区间 C语言解法,注解详细 一看就懂!
字符串
BigDecimal用法常用操作记录
Another implementation of lateral view explode
How much do you know about the mobile APP testing process specifications and methods?
Do you know the basic process and use case design method of interface testing?
latex中复杂公式换行等号对齐
批量修改Shapefile属性表的一种方法(使用gdal.jar)
Cisco common basic configuration of common commands
免费下载天地图全国基础地理信息矢量数据的一种方法
[Personal study summary] CRC verification principle and implementation
IDEA见过就会爱上的超实用快捷键,一键十行!
银联最新测试工程师笔试题目,你能得多少分?
6. The File types
Lecture 4 SVN
Consolidation of Questionnaire Questions and Answers
类 对象 属性 方法 类的成员
Anti App so层对抗分析