当前位置:网站首页>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
边栏推荐
- Sword finger offer II 022 The entry node of the link in the linked list
- 基于ssm 包包商城系统
- Frequently asked interview questions - 3 (operating system)
- Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
- The QT debug version runs normally and the release version runs crash
- Package mall system based on SSM
- Data mining -- understanding data
- Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
- 多线程与高并发(2)——synchronized用法详解
- 尚硅谷 p290 多态性练习
猜你喜欢
软件架构设计——软件架构风格
jdbc入门\获取数据库连接\使用PreparedStatement
MySQL的锁机制
C语言——恶搞关机小程序
引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
手动删除eureka上已经注册的服务
Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
OSI层常用协议
‘EddiesObservations‘ object has no attribute ‘filled‘
lambda表达式
随机推荐
lambda表达式
Introduction to data security -- detailed explanation of database audit system
框架解析2.源码-登录认证
Fletter next generation graphics renderer impaller
Meta annotation (annotation of annotation)
多线程与高并发(2)——synchronized用法详解
POI generates excel and inserts pictures
Pytorch deep learning practice_ 11 convolutional neural network
deep learning object detection
Interview Basics
Character recognition easyocr
Qwebsocket communication
Generation of straightening body in 3D slicer
Linear sieve method (prime sieve)
Hongji | how does HR carry out self change and organizational change in the digital era?
Find the number of "blocks" in the matrix (BFS)
PHP处理json_decode()解析JSON.stringify
Excel obtains the difference data of two columns of data
Hotkeys, interface visualization configuration (interface interaction)
MySQL的锁机制