jdbc连接
DriverManager 驱动程序管理器是负责管理驱动程序的,驱动注册以后,会保存在DriverManager中的已注册列表中后续的处理就可以对这个列表进行操作.
注册驱动方式
1.DriverManager.registerDriver();
2.写代码实现
Class.forName("com.mysql.jdbc.Driver");
利用反射机制
3.com.mysql.jdbc.Driver类中存在静态代码快
源码中自动实现
4.mysql5之后,在jar包中存在一个java.sql.Driver配置文件,文件指定加载com.mysql.cj.jdbc.Driver
通过SPI机制实现。
类加载器、spi 反射技术 javase进阶基础
package com.edu.jou.demo;
import com.mysql.jdbc.Driver;
import java.sql.*;
public class JdbcDemo01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.导入mysql jar
//2.注册mysql驱动 jdbc
//Class.forName("")//反射机制 jdbc 注册驱动/ioc
Class.forName("com.mysql.jdbc.Driver");
//3. 创建数据库连接 协议 http tcp jdbc
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hmk_test?serverTimezone=UTC", "root", "sasa");
//4.获取执行者对象
Statement statement = connection.createStatement();
//5.执行自己编写的sql语句
ResultSet resultSet = statement.executeQuery("select * from hmk_users");
//6.执行sql语句的结果处理操作
while(resultSet.next()){
System.out.println( resultSet.getInt("id") +" "+ resultSet.getString("name") + " " +resultSet.getString("pwd"));
}
//7.关闭这些资源
connection.close();
statement.close();
}
}
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
1 hmk 123
2 bilibili hmk2020
jdbc分层架构
数据库中的数据类型需要与db对应 实体类中的 基本数据类型 建议用包装类 默认是为null
分层架构
com.edu.jou.entity---实体类----创建实体类与数据库表结构字段一一对应的
com.edu.jou.dao----数据库访问层----db打交道
com.edu.jou.serivce---业务逻辑层
com.edu.jou.controller---控制层
如果在db数据类型是为varchar 对应 string
如果在db数据类型是为int对应 Integer
executeUpdate----执行insert 、update、delete sql语句
返回影响行数
实体类层
package com.edu.jou.entity;
public class StudentEntity {
private long id;
private String name;
private Integer age;
private String address;
public StudentEntity(long id, String name, Integer age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "StudentEntity{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
dao层
package com.edu.jou.dao;
import com.edu.jou.entity.StudentEntity;
import java.sql.*;
import java.util.ArrayList;
public class StudentDao {
public ArrayList<StudentEntity> allStudent() {
Statement statement = null;
ResultSet resultSet = null;
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hmk?serverTimezone=UTC", "root", "sasa");
statement = connection.createStatement();
resultSet = statement.executeQuery("select * from hmk_student");
ArrayList<StudentEntity> StudentEntitys = new ArrayList<>();
while (resultSet.next()) {
long id = resultSet.getLong("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
StudentEntity studentEntity = new StudentEntity(id, name, age, address);
StudentEntitys.add(studentEntity);
}
return StudentEntitys;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Service
package com.edu.jou.service;
import com.edu.jou.dao.StudentDao;
import com.edu.jou.entity.StudentEntity;
import java.util.ArrayList;
public class StudentService {
/**?
* 查询所有学生的信息
*/
private StudentDao studentDao = new StudentDao();
public ArrayList<StudentEntity> allStudent(){
ArrayList<StudentEntity> studentEntities = studentDao.allStudent();
//通过业务逻辑层 调用dao层代码
return studentEntities;
}
}
test测试类
package com.edu.jou.test;
import com.edu.jou.entity.StudentEntity;
import com.edu.jou.service.StudentService;
import java.util.ArrayList;
public class StudentTest {
public static void main(String[] args) {
StudentService studentService = new StudentService();
ArrayList<StudentEntity> studentEntities = studentService.allStudent();
for (StudentEntity stu: studentEntities){
System.out.println(stu.toString());
}
}
}
标签:jdbc,name,resultSet,初识,JDBC,edu,com,public
From: https://www.cnblogs.com/hekang520/p/17338603.html