当前位置:网站首页>JDBC JDBC
JDBC JDBC
2022-04-23 02:31:00 【STATICHIT静砸】
DriverManager:用于管理JDBC驱动的服务类。程序中使用该类的主要功能是获取Connection对象,该类包含如下方法:
public static synchronized Connection getConnection(String url,String user,String pass)throws SQLException
//该方法获得url对应数据库的连接
Connection:代表数据库连接对象,每个Connection代表一个物理连接会话。想要访问数据库,必须先获得数据库连接。该接口的常用方法入如下。
Statement creatStatement()throws SQLExcetpion
//该方法返回一个Statement对象
JDBC编程步骤(六大步骤)
1.加载驱动
2.获取连接
3.获取数据库操作对象
4.执行sql
5.处理查询结果集
6.释放资源
1.加载数据库驱动
通常使用Class类的forName()静态方法来加载驱动。例如
Class.forName(driverClass)
代码中的driverClass就是数据库驱动类所对应的字符串。
加载MYSQL的驱动:
Class.forName("com.mysql.cj.jdbc.Driver");
2.获取连接
通过DriverManager获取数据库连接。
//获取数据库连接
DriverManager.getConnection(String url,String user,String pass);
URL:数据库
数据库URL通常遵循如下写法
jdbc:subprotocol:other stuff
上面URL写法中的jdbc是固定的,而subprotocol指定连接到特定数据库的驱动,而后面的other stuff也不是固定的。例如MYSQL数据库的URL写法如下:
jdbc:mysql://hostname:port/databasename
url由哪几部分组成:
协议,IP,端口号,资源名称例如: jdbc:mysql://localhost:3366/bjpowernode
jdbc:mysql:// 协议
localhost IP地址
3366 端口号
chatroom 数据库实例名
3. 获取数据库操作对象
通过Connection对象创建Statement对象。
Connection创建Statement的方法有一下三个
creatStatement():创建基本的Statement对象
prepareStatement(String sql):根据传入的SQL语句创建预编译的Statement对象
prepareCall(String sql):根据传入的SQL语句创建CallableStatement对象
将3.4.操作合并:
eg:
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx", "root", "123456"); //这里输入的是数据库名字xxx Statement state=con.createStatement();
4.执行sql
使用Statement执行SQL语句。所有的Statement都有如下三个方法来执行SQL语句。
execute():可以执行任何SQL语句。但比较麻烦。
executeUpdata():主要用于执行DML和DDL语句。执行DML语句返回受SQL影响的行数,执行DDL语句返回0.
executeQuery():只能执行查询语句,执行后返回代表查询结果的ResultSet对象。
5.处理查询结果集
如果执行的SQL语句是查询语句,则执行结构将返回一个ResultSet对象。该对象里保存了SQL语句查询的结构,程序可以通过操作该REsultSet对象来取出查询结果。(根据情况按需取数据啦)
6.释放资源
回收数据库资源,包括关闭ResultSet,Statement和Connection等资源。
版权声明
本文为[STATICHIT静砸]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_62742402/article/details/124357361
边栏推荐
- 使用Go语言构建Web服务器
- RT_Thread自问自答
- Go language ⌈ mutex and state coordination ⌋
- 【Chrome扩展程序】content_script的跨域问题
- Hack the box optimum
- Network jitter tool clumsy
- [XJTU计算机网络安全与管理]第二讲 密码技术
- 双亲委派模型【理解】
- Class initialization and instance initialization interview questions
- 89 logistic regression user portrait user response prediction
猜你喜欢

005_ redis_ Set set

009_Redis_RedisTemplate入门

Day18 -- stack queue

Network jitter tool clumsy

Global, exclusive and local routing guard

从开源爱好者到 Apache 董事,一共分几步?

使用Go语言构建Web服务器

013_ Analysis of SMS verification code login process based on session

SQL server2019无法下载所需文件,这可能表示安装程序的版本不再受支持,怎么办了
Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
随机推荐
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
How does Axure set the content of the text box to the current date when the page is loaded
Push data from onenet cloud platform to database
How to prevent leakage of operation and maintenance data
006_redis_jedis快速入门
Time. In ANSI standard library H header file
全局、獨享、局部路由守衛
每日一题冲刺大厂第十六天 NOIP普及组 三国游戏
一个国产图像分割项目重磅开源!
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
电源电路设计原来是这么回事
So library dependency
Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
[nk]牛客月赛48 D
Jupyter for local and remote access to ECS
RT_Thread自问自答
JVM类加载器
Handwritten memory pool and principle code analysis [C language]
Develop a chrome plug-in from 0 (2)