当前位置:网站首页>JDBC功能类详解
JDBC功能类详解
2022-07-20 00:36:00 【汤键.】
目录
DriverManager驱动管理对象
1.注册驱动
- 注册给定的驱动程序
- static void registerDriver(Driver driver)
- 在com.mysql.jdbc.Driver类中存在此静态代码块
- 而静态代码块是随着类被使用到也随之使用起来
- 所以可以注册驱动的操作
- 故写代码使用:
- Class.forName("com.mysql.jdbc.Driver");
- 注意:
- 我们不需要通过DriverManager调用静态方法registerDriver(),因为只要Driver类被使用,就会执行其静态代码块完成注册驱动
- MySQL5之后可以省略注册驱动的步骤,在jar包中,存在一个java.sql.Driver配置文件,文件中指定了com.mysql.jdbc.Driver
2.获取数据库连接
- 在其中有这样一个静态方法
- static Connection getConnection(String url, String user, String password)
- 返回值:Connection(数据库连接对象)
- 参数:
- url
- 指定连接的路径
- 语法:jdbc:mysql://ip地址(域名):端口号/数据库名称
- user
- 用户名
- password
- 密码
Connection数据库连接对象
1.获取执行者对象
- 获取普通执行者对象
- Statement create Statement();
- 获取预编译执行者对象
- PrepareStatement prepareStatement(String sql);
2.管理事务
- 开启事务
- setAutoCommit(Boolean autoCommit);
- 参数为false,则开启事务
- 提交事务
- commit();
- 回滚事务
- rollback();
3.释放资源
- 立即将数据库连接对象释放
- void close();
Statement执行sql语句的对象
1.执行DML语句
- int executeUpdate(String sql);
- 返回值:
- 返回影响的行数
2.执行DQL语句
- ResultSet executeQuery(String sql);
- 返回值ResultSet:封装查询的结果
- 参数sql:可以执行select语句
3.释放资源
- 立即将执行者对象释放
- void close();
ResultSet结果集对象
1.判断结果集中是否还有数据
- Boolean next();
- 有数据返回true,并将索引向下移动一行
- 没有数据返回false
2.获取结果集中的数据
- xxx getXxx("列名");
- xxx代表数据类型(要获取某列数据,这一列的数据类型)
- 例如:
- String getString("name");
- int getInt("age");
3.释放资源
- 立即将结果集对象释放:
- void close();
边栏推荐
- 二进制安装MySQL5.7
- What does the performance test report include? Where can I find the template? Look here
- 央视新闻《南京开住宿手撕定额发票》新闻频道_人民网
- Dynamic segment tree leetcode seven hundred and thirty-one
- 央视新闻《郑州开住宿手撕定额发票》新闻频道_人民网
- TiKV & TiFlash 加速复杂业务查询
- Test / development programmer Xiao Zhang's blind date
- XSS跨站脚本攻击
- For statement range function
- Developers must read: 2022 mobile application operation growth insight white paper
猜你喜欢
随机推荐
游戏公司的越冬样本:出海和精品化成为新的增长点?
Cpolar application example helps shipping customers Telecommuting
Common interview questions for app UI automated testing may be useful~
软件测试知识库+1,5款顶级自动化测试工具推荐和使用分析
web测试之功能测试常用的方法有哪几种?有什么要点要注意?
error: ‘for‘ loop initial declarations are only allowed in C99 mode
吴恩达-02 改善深层神经网络:超参数调试、正则化以及优化
快速入门Linxu笔记
10. Gin quick start
method
【技术人才懂的浪漫】TiDB 社区为你准备好了给另一半的“七夕节”礼物,人人都有份哟!
央视新闻《深圳开餐饮手撕定额发票》新闻频道_人民网
模型压缩-方案(一)-参数共享:ALBERT、BERT-of-Theseus
Edge 开发者沙龙|一小时精通Edge扩展开发
央视新闻《宁波开餐饮手撕定额发票》新闻频道_人民网
el-table 用formatter 将接口返回的 多个数字类型的字符串 转换成对应汉字
. Net uses its own Di batch injection service and backgroundservice
QT_代码
Web performance test needs analysis, what should we do?
sudo命令以其他用户执行命令