JDBC
数据持久化(了解即可)
-
数据持久化是指将内存中的数据保存到持久存储设备(如硬盘)上的过程。在Java应用程序中,常用的数据持久化技术包括:
-
关系型数据库:如MySQL、Oracle、PostgreSQL等。
-
对象关系映射(ORM)框架:如Hibernate、MyBatis等。
-
NoSQL数据库:如MongoDB、Cassandra等
-
文件系统:如XML、JSON文件等。
-
-
数据持久化的目的是确保数据在应用程序关闭后仍然存在,并且可以在需要时重新加载。
JDBC介绍---连接数据库的标准
-
JDBC(Java Database Connectivity)是Java平台的标准API,用于执行SQL语句并与数据库进行交互。
-
JDBC为Java应用程序提供了访问各种数据库系统的统一接口,使得开发人员可以编写与数据库无关的应用程
序。
-
在JDBC出现之前,每个数据库厂商都有自己的API和连接方式,这导致开发人员需要为不同的数据库编写不同的代码。
-
JDBC的出现解决了这一问题,通过提供一个标准的API,使得Java应用程序可以轻松地连接和操作不同类型的
数据库。
数据库驱动
-
JDBC通过数据库驱动程序来实现与特定数据库的通信。
每个数据库厂商通常会提供一个JDBC驱动程序,该驱动程序实现了JDBC API,使得Java应用程序可以通过JDBC接口与数据库进行交互。常见的数据库驱动程序包括:
-
DriverManager
-
DriverManager类是JDBC的核心类之一,用于管理和注册驱动程序,并建立数据库连接。
-
getConnection(String url, String user, String password) : 获取数据库连接。
-
-
Connection
-
-
表示与数据库的连接。通过Connection对象,可以创建PreparedStatement对象,用于执行所对应的SQL语句。
-
PreparedStatement
-
用于执行预编译的SQL语句,可以提高性能并防止SQL注入攻击。
-
ResultSet
-
表示查询结果集,提供了遍历和检索结果的方法。常用的方法包括
next() : 移动到下一个记录,到最后一条语句之后返回false
getString(int columnIndex) : 获取指定列的字符串值
getInt(int columnIndex) : 获取指定列的整数值。
-
-
基本步骤
-
需要在编辑器中导入对应的jar包
-
复制粘贴jar包致对应的eclips的包下
-
右击选择Build Path---Add to Build Path
-
jar包消失,下面对应出现的奶瓶包代表已导入
-
自己上网搜索mysql.jar包,下载
-
-
编写连接类---固定---记死
//加载驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getCon() { //这里的Connection需要导包
Connection con=null;
try {
con=DriverManager.getConnection("jdbc:mysql: / localhost:3306/数据库名?
serverTimezone=GMT","用户名","密码"); //填写自己对应的需要修改数据库名
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
-
JDBC实现数据新增
public class Book {
private int id;
private String title;
private double price;
private String description;
}
public int add(Book b) {
try {
//获取连接对象
con=DBHelper.getCon();
//编写sql语句
String sql="insert into book(title,price,description)"+ " values(?,?,?)";
//获取执行对象
ps=con.prepareStatement(sql);
设置?对应值
ps.setString(1, b.getPrice());
ps.setString(2, b.getAuthor());
ps.setString(3, b.getDescription);
//执行sql语句获取结果
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
-
删除学生案例
try {
//获取联接
Connection con = DBHelper.getCon();
//编写sql语句
String str = "delete from students where id = ?";
//连接对象制出执行对象
PreparedStatement pp = con.prepareStatement(str);
//设置对应的值
while(true){
System.out.println("请输入学生的学号");
int kk = sc.nextInt();
pp.setInt(1, kk);
//执行语句,获取结果
int h = pp.executeUpdate();
System.out.println(h);
while(true) {
System.out.println("是否还需要继续删除学生,y/n");
String xx = sc.next();
if(xx.equals("y")) {
i=1;
break;
}else if(xx.equals("n")) {
i=2;
break;
}else {
System.out.println("输入错误");
continue;
}
}
if(i == 1) {
continue;
}else if(i==2) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
JDBC数据查询
-
基本步骤
-
获得连接
-
编写需要执行的sql语句
-
通过sql语句与连接对象制出执行对象
-
执行语句 获得结果
-
ResultSet rs = ps.executeQuery();
-
-
查询结果集
-
遍历结果集 --- 通过列的顺序拿值
-
-
增删改的前四个步骤一样,查,需要通过配合结果集使用
-
查询单个用if,多个用while
-
原始查询
try {
//获得连接
Connection con = DBHelper.getCon();
//编写需要执行的sql语句
String sql = "SELECT id, title, price, description FROM books";
//通过sql语句与连接对象制出执行对象
PreparedStatement ps = con.prepareStatement(sql);
//执行语句 获得结果
ResultSet rs = ps.executeQuery();
//处理结果集
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
double price = rs.getDouble("price");
String description = rs.getString("description");
//输出书籍信息
System.out.println("ID: " + id + ", 标题: " + title + ", 价格: " + price
+ ", 描述: " + description);
}
} catch (Exception e) {
e.printStackTrace();
}
-
基于对象查询
try {
//获得连接
Connection con = DBHelper.getCon();
//编写需要执行的sql语句
String sql = "SELECT id, title, price, description FROM books";
//通过sql语句与连接对象制出执行对象
PreparedStatement ps = con.prepareStatement(sql);
//执行语句 获得结果
ResultSet rs = ps.executeQuery();
//处理结果集
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
double price = rs.getDouble("price");
String description = rs.getString("description");
//创建 Book 对象并设置属性
Book book = new Book(id, title, price, description);
//打印 Book 对象
System.out.println(book);
}
} catch (Exception e) {
e.printStackTrace();
}
-
集合承载数据
List<Books> bookList = new ArrayList > ();
try {
//获得连接
Connection con = DBHelper.getCon();
//编写需要执行的sql语句
String sql = "SELECT id, title, price, description FROM books";
//通过sql语句与连接对象制出执行对象
PreparedStatement ps = con.prepareStatement(sql);
//执行语句 获得结果
ResultSet rs = ps.executeQuery();
//处理结果集
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
double price = rs.getDouble("price");
String description = rs.getString("description");
//创建 Books 对象并设置属性
Books book = new Books(id, title, price, description);
//将 Books 对象添加到列表中
bookList.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}
//输出集合的长度
System.out.println("总共有 " + bookList.size() + " 本书");
//允许用户输入想要查看的书的序号
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您想查看的书的序号:");
int index = scanner.nextInt();
Books book = bookList.get(index);
System.out.println(book);
-
模糊查询的实现
List<Books> bookList = new ArrayList > ();
try {
//获得连接
Connection con = DBHelper.getCon();
//编写需要执行的sql语句
String sql = "SELECT id, title, price, description FROM books WHERE title
LIKE ?";
//通过sql语句与连接对象制出执行对象
PreparedStatement ps = con.prepareStatement(sql);
//设置参数
ps.setString(1, "%" + 关键字 + "%");
//执行语句 获得结果
ResultSet rs = ps.executeQuery();
//处理结果集
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
double price = rs.getDouble("price");
String description = rs.getString("description");
//创建 Books 对象并设置属性
Books book = new Books(id, title, price, description);
//将 Books 对象添加到列表中
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}
-
价格区间查询
List<Books> bookList = new ArrayList > ();
try {
//获得连接
Connection con = DBHelper.getCon();
//编写需要执行的sql语句
String sql = "SELECT id, title, price, description FROM books WHERE price
BETWEEN ? AND ?";
//通过sql语句与连接对象制出执行对象
PreparedStatement ps = con.prepareStatement(sql);
//设置参数
ps.setDouble(1, 价格1);
ps.setDouble(2, 价格2);
//执行语句 获得结果
ResultSet rs = ps.executeQuery();
//处理结果集
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
double price = rs.getDouble("price");
String description = rs.getString("description");
//创建 Books 对象并设置属性
Books book = new Books(id, title, price, description);
//将 Books 对象添加到列表中
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBC数据修改
-
根据id查询单个信息
Scanner sc = new Scanner(System.in);
//获取用户输入的书籍编号
System.out.print("请输入您想修改的书的编号:");
int bookId = sc.nextInt();
Books book = null;
try {
//获得连接
Connection con = DBHelper.getCon();
//编写需要执行的sql语句
String sql = "SELECT id, title, price, description FROM books WHERE id =
?";
//通过sql语句与连接对象制出执行对象
PreparedStatement ps = con.prepareStatement(sql);
//设置参数
ps.setInt(1, bookId);
//执行语句 获得结果
ResultSet rs = ps.executeQuery();
//处理结果集
if (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
double price = rs.getDouble("price");
String description = rs.getString("description");
//创建 Books 对象并设置属性
book = new Books(id, title, price, description);
}
} catch (Exception e) {
e.printStackTrace();
}
if (book = null) {
System.out.println("查无此书");
return;
}
-
根据id修改信息
//获取用户输入的新信息
System.out.print("请输入新的标题(当前标题:" + book.getTitle() + "):");
String nt = sc.next();
System.out.print("请输入新的价格(当前价格:" + book.getPrice() + "):");
double np = sc.nextDouble();
System.out.print("请输入新的描述(当前描述:" + book.getDescription() + "):");
String nd = sc.next();
try {
//获得连接
Connection con = DBHelper.getCon();
//编写需要执行的sql语句
String sql = "UPDATE books SET title = ?, price = ?, description = ? WHERE
id = ?";
//通过sql语句与连接对象制出执行对象
PreparedStatement ps = con.prepareStatement(sql);
//设置参数
ps.setString(1, nt);
ps.setDouble(2, np);
ps.setString(3, nd);
ps.setInt(4, bookId);
//执行更新
int n = ps.executeUpdate();
if (n > 0) {
System.out.println("书籍信息已成功更新。");
} else {
System.out.println("书籍信息更新失败。");
}
} catch (Exception e) {
e.printStackTrace();
}
2万+

被折叠的 条评论
为什么被折叠?



