当前位置:网站首页>JDBC的入门使用
JDBC的入门使用
2022-08-02 02:56:00 【兄dei!】
JDBC概念:Java DataBase Connectivity Java 数据库连接, Java语言操作数据库
其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。
使用步骤:
1. 导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
1.复制mysql-connector-java-5.1.37-bin.jar到项目的libs目录下
2.右键-->Add As Library
2. 注册驱动
3. 获取数据库连接对象 Connection
4. 定义sql
5. 获取执行sql语句的对象 Statement
6. 执行sql,接受返回结果
7. 处理结果
8. 释放资源
Connection connection=null;
PreparedStatement statement=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB1","root","root");
//定义sql
String sql="select * from user ";
//执行sql 接收返回结果--statement对象
statement = connection.prepareStatement(sql);
//获取查询结果集resultSet
ResultSet resultSet = statement.executeQuery(sql);
//拿到结果集的数据
while(resultSet.next()){
int id=resultSet.getInt(1);//可传列数
System.out.println("id:"+id);
String name = resultSet.getString(2);
System.out.println("username:"+name);
String password = resultSet.getString("password");//也可直接传列名
System.out.println("password:"+password);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
finally {
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}JDBCUtils工具类
配置文件:
url=jdbc:mysql://localhost:3306/DB1
user=root
password=root
driver=com.mysql.jdbc.Driverpublic class JDBCUtils {
//JDBC工具类 直接获取连接对象,数据库url直接写在配置文件里面
private static String url;
private static String user;
private static String password;
private static String driver;
//使用静态代码块
static {
try {
//读取配置文件
Properties pro=new Properties();
URL resource = JDBCUtils.class.getClassLoader().getResource("jdbc.properties");
String path = resource.getPath();
System.out.println(path);
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
pro.load(is);
url=pro.getProperty("url");
user= pro.getProperty("user");
password= pro.getProperty("password");
driver=pro.getProperty("driver");
Class.forName(driver);//注册驱动
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接对象connection
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//释放资源
public static void Close(Statement statement,Connection connection){
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}测试:
//使用工具类获取连接对象
Connection coon=JDBCUtils.getConnection();
//定义Sql语句
String sql="select * from user where username =? and password = ?";
//获取执行对象(防注入)
PreparedStatement preparedStatement = coon.prepareStatement(sql);
Scanner in = new Scanner(System.in);
System.out.println("请输入用户名");
String username=in.next();
System.out.println("请输入密码");
String password=in.next();
//传参数(?的位置,传入的数据)
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
ResultSet set = preparedStatement.executeQuery();
if(set.next()){
System.out.println("登录成功!");
}
else System.out.println("登陆失败!");
//释放资源
in.close();
JDBCUtils.Close(preparedStatement,coon);数据库user表



边栏推荐
- 【LeetCode】206. Reverse linked list
- 详解最强分布式锁工具:Redisson
- 7-44 基于词频的文件相似度 (30 分)
- 总体写作原则
- MySQL六脉神剑,SQL通关大总结
- 【LeetCode】83.删除排序链表中的重复元素
- [LeetCode] 94. Inorder traversal of binary tree
- [Daily LeetCode]——1. The sum of two numbers
- OperatingSystemMXBean to get system performance metrics
- Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
猜你喜欢

MySQL8--Windows下使用msi(图形界面)安装的方法

PHP WebSehll backdoor script and detection tool

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

How ReentrantLock works

mysql8.0.28 download and installation detailed tutorial, suitable for win11

面试必备!TCP协议经典十五连问!

KICAD 拉线宽度无法修改,解决方法

Common SQL interview questions: 50 classic examples

Nacos source code analysis topic (1) - environment preparation

Difference between #{} and ${}
随机推荐
ApiFox 基本使用教程(浅尝辄止,非广)
IPIDEA的使用方式
22-08-01 西安 尚医通(01)跨域配置、Swagger2、R类、统一异常处理和自定义异常、Logback日志
PHP WebSehll 后门脚本与检测工具
【LeetCode】144. Preorder Traversal of Binary Tree
IPFS部署及文件上传(golang)
非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
【LeetCode】20.有效的括号
2022牛客多校四_G M
analog IC layout-Design for reliability
svm.SVC application practice 1--Breast cancer detection
2W字!梳理50道经典计算机网络面试题(收藏版)
2W字!详解20道Redis经典面试题!(珍藏版)
递归检查配置项是否更变并替换
7-44 基于词频的文件相似度 (30 分)
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
JSP Webshell 免杀
给你一个大厂面试的机会,你能面试上吗?进来看看!
MySQL8--Windows下使用msi(图形界面)安装的方法
【LeetCode】104. Maximum depth of binary tree