当前位置:网站首页>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
边栏推荐
- 002_Redis_String类型常见的操作命令
- [XJTU計算機網絡安全與管理]第二講 密碼技術
- 定了,今日起,本号粉丝可免费参与网易数据分析培训营!
- They are all intelligent in the whole house. What's the difference between aqara and homekit?
- 【ValueError: math domain error】
- Dynamic batch processing and static batch processing of unity
- Develop a chrome plug-in from 0 (2)
- wordpress 调用指定页面内容详解2 get_children()
- tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
- SQL server2019 cannot download the required files, which may indicate that the version of the installer is no longer supported. What should I do
猜你喜欢
Global, exclusive and local routing guard
005_ redis_ Set set
A domestic image segmentation project is heavy and open source!
006_redis_SortedSet类型
009_ Redis_ Getting started with redistemplate
Talk about biology live broadcast: Dr. Wang Ziyuan, a lake view biology, exploring hepatitis B with gene therapy
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售
下载正版Origin Pro 2022 教程 及 如何 激 活
Day18 -- stack queue
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
随机推荐
IAR embedded development stm32f103c8t6 Lighting LED
openstack 服务的启动
Jupyter for local and remote access to ECS
Target narak
Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
[untitled]
Startup of openstack service
手写内存池以及原理代码分析【C语言】
Applet canvas canvas half ring
高效音乐格式转换工具Music Converter Pro
[XJTU計算機網絡安全與管理]第二講 密碼技術
[XJTU计算机网络安全与管理]第二讲 密码技术
JSP page nesting
Develop a chrome plug-in from 0 (2)
IAR嵌入式开发STM32f103c8t6之点亮LED灯
Flink real-time data warehouse project - Design and implementation of DWS layer
RT_Thread自问自答
JVM运行时数据区(一)
New book recommendation - IPv6 technology and application (Ruijie version)