IDEA实现JDBC的操作步骤
作者:薛定谔的痘痘 时间:2021-08-29 11:08:55
什么是JDBC
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。
JDBC本质
JDBC接口:是由sun公司提供的一套jdbc接口,该接口由各大数据库厂家实现,最终向程序员和用户提供和数据库的交互。
驱动:驱动是由各数据库厂家遵循接口所实现的各个实现类。
IDEA配置
1、IDEA新建工程
2、新建modle
3、添加mysql数据库驱动
3.1鼠标右键点集新建好的modle------>Open Module Setting
3.2 libraries—>±—>Java
3.3 添加mysql数据库驱动
3.3 添加成功
3.4 查看驱动是否配置成功------> ExternalLibraries
JDBC简单实现一条sql语句
代码
package com.test.jdbc;
/**
* @author pan
* @date 2022/1/28 18:52
*/
import com.sun.java.util.jar.pack.DriverResource;
import java.sql.*;
import java.util.ResourceBundle;
* @ClassName : com.test.jdbc.jdbcTest04
* @Description : 类描述
public class JdbcTest04 {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
ResourceBundle resourceBundle = ResourceBundle.getBundle("com\\test\\jdbc\\jdbc");
//1、注册驱动
/*
Driver driver = new com.mysql.cj.jdbc.Driver();
DriverManager.registerDriver(driver);
*/
String aClass = resourceBundle.getString("class");
// 利用类的加载机制可以使完成注册驱动,因为Driver类有一个静态代码块,该代码块完成了上述的任务,可以利用反射机制,在类加载的时候执行静态代码块
Class.forName(aClass);
//2、建立连接
String url = resourceBundle.getString("url");
String user = resourceBundle.getString("user");
String password = resourceBundle.getString("password");
connection = DriverManager.getConnection(url, user, password);
//3、获取数据库操作对象
statement = connection.createStatement();
//4、执行sql语句
String sql = "select * from emp";
resultSet = statement.executeQuery(sql);
//5、操作结果集
while(resultSet.next()){
String ename = resultSet.getString("ename");
String deptno = resultSet.getString("deptno");
String sal = resultSet.getString("sal");
System.out.println(ename+" " +deptno+" "+ sal);
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}finally {
//6、释放资源
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(statement != null){
statement.close();
if(connection != null){
connection.close();
}
}
}
jdbc.properties
url = jdbc:mysql://localhost:3306/yinpan?useSSl=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
user = root
password =
class = com.mysql.cj.jdbc.Driver
执行结果
来源:https://blog.csdn.net/qq_44859600/article/details/122727206
标签:IDEA,JDBC
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#接口INotifyPropertyChanged使用方法
2021-11-22 13:33:53
![](https://img.aspxhome.com/file/2023/6/66576_0s.jpg)
关于SpringMVC在Controller层方法的参数解析详解
2022-10-29 01:06:10
C#创建临时文件的方法
2023-06-16 14:32:36
SpringBoot注解梳理(小结)
2023-11-10 13:27:19
java web中 HttpClient模拟浏览器登录后发起请求
2022-10-27 23:07:05
hadoop运行java程序(jar包)并运行时动态指定参数
2023-07-27 11:02:10
![](https://img.aspxhome.com/file/2023/9/57829_0s.png)
springmvc如何使用POJO作为参数
2021-06-02 00:29:46
![](https://img.aspxhome.com/file/2023/1/61831_0s.png)
java基础的详细了解第九天
2022-11-11 20:53:50
![](https://img.aspxhome.com/file/2023/0/64950_0s.png)
java开源调度如何给xxljob加k8s执行器
2021-09-17 16:41:50
![](https://img.aspxhome.com/file/2023/9/62229_0s.png)
elasticsearch head的安装及使用过程解析
2021-10-10 10:24:59
![](https://img.aspxhome.com/file/2023/2/79942_0s.png)
java 值Document解析xml详细介绍
2021-11-13 15:59:00
![](https://img.aspxhome.com/file/2023/1/79921_0s.png)
Java深入了解数据结构之栈与队列的详解
2022-03-24 08:12:27
![](https://img.aspxhome.com/file/2023/0/61590_0s.png)
java equals函数用法详解
2022-07-31 06:40:20
Spring boot集成redis lettuce代码实例
2022-08-03 12:21:19
Java的关键字与保留字小结
2021-05-30 10:20:50
Java语法中Lambda表达式无法抛出异常的解决
2022-10-13 01:04:43
Java通过XPath获取XML文件中符合特定条件的节点
2023-01-19 07:42:34
C#实现读取注册表监控当前操作系统已安装软件变化的方法
2022-01-04 08:07:04
Java11中基于嵌套关系的访问控制优化详解
2021-12-28 18:54:20
java锁synchronized面试常问总结
2023-08-01 05:11:37
![](https://img.aspxhome.com/file/2023/7/57917_0s.png)