当前位置:网站首页>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表



边栏推荐
猜你喜欢
随机推荐
MySQL8.0.28 installation tutorial
AcWing 1285. Word Problem Solving (AC Automata)
MySQL8.0.28安装教程
analog IC layout-Environmental noise
JSP Webshell free kill
WebShell特征值汇总与检测工具
【LeetCode】94.二叉树的中序遍历
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
ASP WebShell backdoor script and anti-kill
因为WiFi原因navicat 无法连接数据库Mysql
Chapter 7 Noise analysis
【LeetCode】145. Postorder Traversal of Binary Tree
How ReentrantLock works
第一章——线性表(顺序表和链表)
第二章——堆栈、队列
Tree Chain Segmentation-
给你一个大厂面试的机会,你能面试上吗?进来看看!
Chapter 11_Database Design Specifications
Reasons and solutions for Invalid bound statement (not found)
KICAD 小封装拉线卡顿问题 解决方法









