当前位置:网站首页>JDBC连接数据库
JDBC连接数据库
2022-04-23 05:42:00 【hanyc..】
主要步骤:
1、加载数据库驱动
2、获取数据库连接(包含了获取用户信息、url)
3、创建执行sql的对象
4、执行sql语句
5、返回执行的结果集
6、释放连接
package com.hyc.study01;
import java.sql.*;
public class JdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取用户信息、url
//jdbcstudy是要连接的数据库的名称
//useUnicode=true&characterEncoding=utf8&useSSL=true 支持中文编码&设定中文字符集为utf8&使用安全的连接
String url =
"jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String psw = "123456";
//3、获取数据库连接
//这里的Connection就代表了数据库,如果返回的对象不为空,代表数据库就拿到了
Connection connection = DriverManager.getConnection(url, username, psw);
//4、创建用来执行sql的对象
Statement statement = connection.createStatement();
//5、执行sql语句
//6、返回执行结果集
String sql = "SELECT * FROM users";
//执行查询sql语句,得到返回的结果集
ResultSet resultSet = statement.executeQuery(sql);//返回的结果集是一个链表的形式
while (resultSet.next()) {
System.out.println("id=" + resultSet.getInt("id"));
System.out.println("name=" + resultSet.getString("NAME"));
System.out.println("psw=" + resultSet.getString("PASSWORD"));
System.out.println("email=" + resultSet.getString("email"));
System.out.println("birth=" + resultSet.getDate("birthday"));
System.out.println("=====================================================");
}
//int num = statement.executeUpdate("add delete update sql");
//增删改都用statement.executeUpdate(),该方法返回执行sql语句受影响的行数
//7、释放连接
resultSet.close();
statement.close();
connection.close();
}
}
数据库以及数据库表:


版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42732184/article/details/124215610
边栏推荐
- 尚硅谷 p290 多态性练习
- Hotkeys, interface visualization configuration (interface interaction)
- 一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
- shell指令学习1
- POI exports to excel, and the same row of data is automatically merged into cells
- Package mall system based on SSM
- Typescript interface & type rough understanding
- 手动删除eureka上已经注册的服务
- MySQL realizes master-slave replication / master-slave synchronization
- xxl-job采坑指南xxl-rpc remoting error(connect timed out)
猜你喜欢

Generation of straightening body in 3D slicer

JVM系列(3)——内存分配与回收策略

Fletter next generation graphics renderer impaller

Deep learning object detection

SQL statement simple optimization

2-软件设计原则

JVM系列(4)——内存溢出(OOM)

引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启

Frequently asked interview questions - 2 (computer network)

基于ssm 包包商城系统
随机推荐
Isosceles triangle - the 9th Lanqiao provincial competition - group C
mysql sql优化之Explain
对象转map
2 - software design principles
Shell instruction learning 1
提升Facebook触及率和互动率攻略 | 智能客服帮您抓住用户的心
Breadth first search topics (BFS)
Qwebsocket communication
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
Add days to date
2.devops-sonar安装
ES6之解构函数
No.1.#_ 6 Navicat shortcuts
2-软件设计原则
Hongji | how does HR carry out self change and organizational change in the digital era?
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
C language - Spoof shutdown applet
The list attribute in the entity is empty or null, and is set to an empty array
Excel sets row and column colors according to cell contents
手动删除eureka上已经注册的服务