* 1)学生类中 的属性需要和数据库表中对应上
* Student
* id,name,gender,email
* 2)定义接口StudentDao以及实现类StudentDaoImpl
* 3)实现层:
* /操作数据库
* //执行查询
* //查询一行,将这条学生信息封装到学生类中
* //将学生添加到集合中
* //返回集合List
url=jdbc:mysql://localhost:3306/ee_2106?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
driverClass=com.mysql.cj.jdbc.Driver
user=root
password=123456
public class JdbcUtils {
private static String url = null ;
private static String user = null ;
private static String password = null ;
private static String drivceClass = null ;
//构造方法私有化
private JdbcUtils(){}
//静态代码块
//JdbcUtils工具类一加载就执行静态代码块
static{
try {
//创建一个Properties属性列表
Properties prop = new Properties() ;
//读取src下面的jdbc.properties配置文件
//直接获取当前类的字节码文件对象,获取类加载,获取资源文件所在的输入流对象
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//将流中内容加载到属性列表中
prop.load(inputStream);
System.out.println(prop);//测试属性列表中的内容
//通过key获取value
url = prop.getProperty("url") ;
user = prop.getProperty("user") ;
password = prop.getProperty("password") ;
drivceClass = prop.getProperty("driverClass") ;
//注册驱动
Class.forName(drivceClass) ;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//提供功能都是static
//通用的功能:获取数据库连接对象
public static Connection getConnection(){
Connection conn = null ;
try {
//通过驱动类管理类获取
conn = DriverManager.getConnection(url,user,password) ;
return conn ;
} catch (SQLException e) {
e.printStackTrace();
}
return null ;
}
//关闭资源:
//DDL语句或者DML语句----->Statement对象和Connection
public static void close(Statement stmt,Connection conn){
close(null,stmt,conn);
}
//关闭资源
//DQL语句---->ResultSet对象,Statement对象和Connection
public static void close(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt !=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection connection = JdbcUtils.getConnection();
System.out.println(connection);
}
}
public class Student {
/*
Field Type Comment
id int NOT NULL
name varchar(20) NULL
gende rvarchar(10) NULL
email varchar(50) NULL*/
private int id ; //编号
private String name ;//姓名
private String gender ;//性别
private String email ; //邮箱
public Student() {
}
public Student(int id, String name, String gender, String email) {
this.id = id;
this.name = name;
this.gender = gender;
this.email = email;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
}
public interface StudentDao {
/**
* 查询所有学生信息
* @return 返回学生列表数据
*/
List findAll() ;
/**
* /通过学生id学号查询某个学生信息
* @param id 学号id
* @return 返回的某个学生实体
*/
Student findById(int id) ;
/**
* 添加学生信息
* @param student 学生实体类
*/
void add(Student student) ;
/**
* 更新学生信息
* @param student
*/
void updateStudent(Student student) ;
/**
* 通过学号删除学生
* @param id 学生id号
*/
void delete(int id) ;
}
public class StudentDaoImpl implements StudentDao {
@Override
public List findAll() {
Connection conn = null ;
Statement stmt = null ;
ResultSet rs = null ;
//操作数据库
try {
//创建List集合对象
List list = new ArrayList<>() ;
//获取连接对象
conn = JdbcUtils.getConnection();
//准备sql语句
String sql = "select * from student" ;
//获取执行对象
stmt = conn.createStatement();
//执行查询
rs = stmt.executeQuery(sql);
//声明学生类型的变量student
Student student = null ;
while(rs.next()){
//创建学生对象,并封装学生数据
student = new Student() ;
//查询一行,将这条学生信息封装到学生类中
int id = rs.getInt("id");
String name = rs.getString("name");
String gender = rs.getString("gender") ;
String email = rs.getString("email");
student.setId(id);
student.setName(name);
student.setGender(gender);
student.setEmail(email);
//将学生添加到集合中
//返回集合List
list.add(student) ;
}
return list ;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public Student findById(int id) {
return null;
}
@Override
public void add(Student student) {
}
@Override
public void updateStudent(Student student) {
}
@Override
public void delete(int id) {
}
}
public class JdbcDemo2 {
public static void main(String[] args) throws Exception {
//接口多态方式
//遍历集合,获取结果
/* StudentDao sd = new StudentDaoImpl() ;
List list = sd.findAll();
for (Student student : list) {
//System.out.println(student);
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getGender()+"\t"+student.getEmail()); //toString()
}*/
//反射创建当前StudentDaoImpl的实例
Class clazz = Class.forName("com.qf.jdbc_statment_03_jdbcutils.StudentDaoImpl");
//创建当前类实例
Object obj = clazz.newInstance(); //或者Constructor类对象
//获取Method类对象
Method method = clazz.getMethod("findAll");
//调用
Object result = method.invoke(obj); //toString()
System.out.println(result);
}
}