通过JDBC实现对数据库数据的增删改查操作
1.JDBC介绍Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。由于各大数据库厂商提供的驱动各不相同,导致开发人员学习成本高,sun公司为了简化数据库的操作,提供了一套规范,本质上就是一堆接口,要求各大数据库厂商实现此接口,此后只需学会JDBC这套接口
1.JDBC介绍
Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。由于各大数据库厂商提供的驱动各不相同,导致开发人员学习成本高,sun公司为了简化数据库的操作,提供了一套规范,本质上就是一堆接口,要求各大数据库厂商实现此接口,此后只需学会JDBC这套接口就可以使用所有的数据库驱动了。
数据库驱动:数据库厂商提供的用来操作数据库的jar包(服务器通过jdbc访问数据库的大致流程如下图)

注意:JDBC是由两个包组成,java.sql和javax.sql,这两个jar包以及集成到javase中,但是在开发时,还需要导入具体数据库的驱动jar包(其中一个版本的数据库驱动jar包如下图)
![]()
2.驱动包的导入
1.创建lib目录,将mysql-connector-java-5.0.8-bin.jar拷贝到lib目录下
2.选中jar包右键->add as library->ok


3.操作步骤
- 注册数据库驱动
- 获取连接
- 获取传输器
- 通过传输器发生sql,返回结果
- 处理结果
- 释放资源
mysql 与 oracle 的驱动与连接信息示例:
(数据库名称,账号密码等根据实际自行修改为对应即可)
1.驱动
mysql:com.mysql.jdbc.Driver
oracle:oracle.jdbc.driver.OracleDriver
2.url
mysql:jdbc:mysql://localhost:3306/test
oracle:jdbc:oracle:thin:@localhost:1521:orcl
3.账号密码
mysql:user--root password--root
oracle:user--scott password--trigger
4.编程实现JDBC对数据库的CRUD操作
说明:下面例子将逐个实现对数据库的CRUD操作,其中涉及的资源关闭,获取连接代码出现了复用,故提取一个公共的类JdbcUtils,为了方便调用,将其中的方法定义为静态方法。传输器使用Statement(有sql注入风险,PreparedStatement为其子类,无sql 注入风险)
公共类JdbcUtils 代码:
import java.sql.*;
/**
* jdbc 工具类
*
* @author ww
* @date 2020/6/12 14:26
*/
public class JdbcUtils {
/**
* 资源关闭方法
*
* @param rs
* @param connection
* @param statement
*/
public static void close(ResultSet rs,
Connection connection,
Statement statement) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
rs = null;
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
statement = null;
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
connection = null;
}
}
}
/**
* 获取连接方法
* @return
*/
public static Connection getConnection() {
{
//1.注册数据库驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
return connection;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}
实现查询操作:
public class SelectJdbc {
public static void main(String[] args) {
ResultSet rs = null;
Connection connection = null;
Statement statement = null;
try {
connection = JdbcUtils.getConnection();
statement = connection.createStatement();
//4.通过传输器发生sql ,返回结果
String sql = "select * FROM exa where name ='aimiy4'";
//用接收查询的数据
rs = statement.executeQuery("select * from exa");
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
double math = rs.getDouble("math");
System.out.println(id+"$$"+name+"$$"+math);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//6.释放资源
JdbcUtils.close(rs,connection,statement);
}
}
}
实现插入操作:
public class InsertJdbc {
public static void main(String[] args) {
ResultSet rs = null;
Connection connection = null;
Statement statement = null;
try {
connection =JdbcUtils.getConnection();
statement = connection.createStatement();
//4.通过传输器发生sql ,返回结果
String sql = "insert into exa values (null ,'ayniooo',23,56,89)";
//执行增删改操作,返回值为int --被影响的行数,不用接收
System.out.println("executeUpdate:"+ statement.executeUpdate(sql));
} catch (Exception e) {
e.printStackTrace();
}finally {
//6.释放资源
JdbcUtils.close(rs,connection,statement);
}
}
}
实现更新操作:
public class UpdateJdbc {
public static void main(String[] args) {
ResultSet rs = null;
Connection connection = null;
Statement statement = null;
try {
connection = JdbcUtils.getConnection();
statement = connection.createStatement();
//4.通过传输器发生sql ,返回结果
String sql = "update exa set math=100 where name ='aimiy8'";
//执行增删改操作,返回值为int --被影响的行数,不用接收
System.out.println("executeUpdate:"+ statement.executeUpdate(sql));
} catch (Exception e) {
e.printStackTrace();
}finally {
//6.释放资源
JdbcUtils.close(rs,connection,statement);
}
}
}
实现删除操作:
public class DeleteJdbc {
public static void main(String[] args) {
ResultSet rs = null;
Connection connection = null;
Statement statement = null;
try {
connection = JdbcUtils.getConnection();
statement = connection.createStatement();
//4.通过传输器发生sql ,返回结果
String sql = "delete from exa where name ='ayniooo'";
//执行增删改操作,返回值为int --被影响的行数,不用接收
System.out.println("executeUpdate:"+ statement.executeUpdate(sql));
} catch (Exception e) {
e.printStackTrace();
}finally {
//6.释放资源
JdbcUtils.close(rs,connection,statement);
}
}
}
更多推荐


所有评论(0)