当前位置:网站首页>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表
边栏推荐
- 暴力破解全攻略
- 7、MySQL Workbench 导出导入数据库
- 【CNN记录】tensorflow slice和strided_slice
- Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
- MySQL8--Windows下使用压缩包安装的方法
- AcWing 1285. Word Problem Solving (AC Automata)
- 合奥科技网络 面试(含参考答案)
- Navicat cannot connect to database Mysql because of WiFi
- 2022牛客多校三_F G
- 非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
猜你喜欢
MySQL8.0.28 installation tutorial
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
WebShell特征值汇总与检测工具
- daily a LeetCode 】 【 9. Palindrome
Hit the programmer interview scene: What did Baidu interviewers ask me?
JunitTest单元测试
Nacos source code analysis topic (2) - service registration
Tree Chain Segmentation-
CentOS7安装Oracle数据库的全流程
centos安装mysql8
随机推荐
OperatingSystemMXBean to get system performance metrics
就瞎写=感想
aws s3上传文件
Curriculum Vitae;CV
[LeetCode] 94. Inorder traversal of binary tree
Go简单实现协程池
node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module ‘./scope‘
Nacos source code analysis topic (2) - service registration
【LeetCode】83.删除排序链表中的重复元素
Reasons and solutions for Invalid bound statement (not found)
VPS8505 微功率隔离电源隔离芯片 2.3-6V IN /24V/1A 功率管
【LeetCode】1374. Generate a string with an odd number of each character
PHP WebSehll 后门脚本与检测工具
常见的SQL面试题:经典50例
IPFS deployment and file upload (golang)
PHP WebSehll backdoor script and detection tool
非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
知识体系树
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现