目录结构
建立jdbc.properties
user=root
password=123456
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
driverClass=com.mysql.cj.jdbc.Driver
导入驱动,connector-java....
创建JDBCUtil.java工具类
package com.geng.util;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
public static Connection getConnection(){
Connection conn = null;
try {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeResource(Connection conn, PreparedStatement ps, ResultSet resultSet){
try {
if(resultSet != null)
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(ps != null)
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(conn != null)
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
创建查询测试类
package com.geng.query;
import com.geng.bean.Customer;
import com.geng.util.JDBCUtils;
import org.junit.jupiter.api.Test;
import java.sql.*;
public class testQuery {
@Test
public void test1(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select id, name, email, birth from customers where id = ?";
ps = conn.prepareStatement(sql);
ps.setObject(1, 1);
resultSet = ps.executeQuery();
if(resultSet.next()){
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
String email = resultSet.getString(3);
Date date = resultSet.getDate(4);
Customer customer = new Customer(id, name, email, date);
System.out.println(customer);
}
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, resultSet);
}
}
}
把接收的数据封装成一个对象接收,体现了ORM思想
ORM思想:
一个数据库表对应一个Java类;
一条记录对应一个Java对象;
一个字段对应一个Java类中的属性。
Customer类
package com.geng.bean;
import java.sql.Date;
public class Customer {
private int id;
private String name;
private String email;
private Date date;
public Customer() {
}
public Customer(int id, String name, String email, Date date) {
this.id = id;
this.name = name;
this.email = email;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", date=" + date +
'}';
}
}
执行效果: