当前位置:网站首页>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.Driver
public 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表
边栏推荐
- 非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
- svm.SVC应用实践1--乳腺癌检测
- DVWA安装教程(懂你的不懂·详细)
- Reasons and solutions for Invalid bound statement (not found)
- (一)Redis: 基于 Key-Value 的存储系统
- JSP Webshell 免杀
- AcWing 1053. Repair DNA problem solution (state machine DP, AC automata)
- IPFS部署及文件上传(golang)
- Invalid bound statement (not found)出现的原因和解决方法
- 数仓:数仓从ETL到ELT架构的转化以及俩者的区别
猜你喜欢
pyqt上手体验
1. 获取数据-requests.get()
JSP WebSehll 后门脚本
MySQL8.0.28安装教程
ApiFox 基本使用教程(浅尝辄止,非广)
- daily a LeetCode 】 【 9. Palindrome
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
Chapter 11_Database Design Specifications
aws s3上传文件
Webshell上传方式
随机推荐
mysql8.0.28 download and installation detailed tutorial, suitable for win11
PHP WebSehll 后门脚本与检测工具
(一)Redis: 基于 Key-Value 的存储系统
iVX低代码平台系列详解 -- 概述篇(二)
知识体系树
Chrome浏览器无法加载已解压的.crx文件的解决办法
【LeetCode】144. Preorder Traversal of Binary Tree
【LeetCode】145.二叉树的后序遍历
Navicat cannot connect to database Mysql because of WiFi
MySQL八股文背诵版
树链剖分-
Invalid bound statement (not found)出现的原因和解决方法
PAT甲级打卡-1001-1004
KICAD 小封装拉线卡顿问题 解决方法
KICAD 拉线宽度无法修改,解决方法
leetcode 143. 重排链表
ASP WebShell backdoor script and anti-kill
MySQL8--Windows下使用压缩包安装的方法
How ReentrantLock works
ASP WebShell 后门脚本与免杀