微信登录

JDBC - PS - 查 - 封装ORM - 1sql1表多列1行

ORM

对象关系映射(英语:Object Relational Mapping)
O - Object - 对象 - 1表customers、1个类Customer - 数据存在这里
R - Relational - 关系
M - Mapping - 映射 - 反射 - 反射类的设定的参数

作用

通过rsmd.getColumnLabel(i + 1),做到不定列

O - Object - 对象

创建/使用com.atguigu3.bean包

  1. package com.atguigu3.bean;
  2. import java.sql.Date;
  3. /*
  4. * ORM编程思想 (object relational mapping)
  5. * 一个数据表对应一个java类
  6. * 表中的一条记录对应java类的一个对象
  7. * 表中的一个字段对应java类的一个属性
  8. *
  9. */
  10. public class Customer {
  11. private int id;
  12. private String name;
  13. private String email;
  14. private Date birth;
  15. public Customer() {
  16. super();
  17. }
  18. public Customer(int id, String name, String email, Date birth) {
  19. super();
  20. this.id = id;
  21. this.name = name;
  22. this.email = email;
  23. this.birth = birth;
  24. }
  25. public int getId() {
  26. return id;
  27. }
  28. public void setId(int id) {
  29. this.id = id;
  30. }
  31. public String getName() {
  32. return name;
  33. }
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37. public String getEmail() {
  38. return email;
  39. }
  40. public void setEmail(String email) {
  41. this.email = email;
  42. }
  43. public Date getBirth() {
  44. return birth;
  45. }
  46. public void setBirth(Date birth) {
  47. this.birth = birth;
  48. }
  49. @Override
  50. public String toString() {
  51. return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", birth=" + birth + "]";
  52. }
  53. }
Java类型 SQL类型
boolean BIT
byte TINYINT
short SMALL .INT
int INTEGER
long BIGINT
String CHAR,VARCHAR, LONGVARCHAR
byte array BINARY,VAR BINARY
java.sql.Date DATE
java.sql.Time TIME

逻辑

  1. package com.atguigu3.preparedstatement.crud;
  2. import java.lang.reflect.Field;
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import org.junit.Test;
  10. import com.atguigu3.bean.Customer;
  11. import com.atguigu3.util.JDBCUtils;
  12. /**
  13. *
  14. * @Description 针对于customers表的通用的查询操作
  15. * @author shkstart
  16. * @throws Exception
  17. * @date 上午10:23:40
  18. */
  19. public Customer queryForCustomers(String sql,Object...args){
  20. Connection conn = null;
  21. PreparedStatement ps = null;
  22. ResultSet rs = null;
  23. try {
  24. conn = JDBCUtils.getConnection();
  25. ps = conn.prepareStatement(sql);
  26. for(int i = 0;i < args.length;i++){
  27. ps.setObject(i + 1, args[i]);
  28. }
  29. rs = ps.executeQuery();//prepareStatement通过executeQuery获得数据
  30. ResultSetMetaData rsmd = rs.getMetaData();//获取结果集的元数据 :ResultSetMetaData
  31. int columnCount = rsmd.getColumnCount();//通过ResultSetMetaData获取结果集中的列数
  32. if(rs.next()){
  33. Customer cust = new Customer();//一个Customer类对象
  34. //处理结果集一行数据中的每一个列
  35. for(int i = 0;i <columnCount;i++){
  36. Object columValue = rs.getObject(i + 1);//获取列值
  37. // String columnName = rsmd.getColumnName(i + 1);//获取sql结果的每个列的列名
  38. String columnLabel = rsmd.getColumnLabel(i + 1);//获取sql结果的每个列的别名
  39. //给cust对象指定的columnName属性,赋值为columValue:通过反射
  40. Field field = Customer.class.getDeclaredField(columnLabel);
  41. field.setAccessible(true);
  42. field.set(cust, columValue);
  43. }
  44. return cust;
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }finally{
  49. JDBCUtils.closeResource(conn, ps, rs);
  50. }
  51. return null;
  52. }

getColumnName(i + 1)
getColumnLabel(i + 1)
sql可以换名,getColumnName获取表的名,getColumnLabel可以获取表的名,也可以获取sql换过的名

return cust;//返回Customer对象,而且已经有数据了

使用

  1. package com.atguigu3.preparedstatement.crud;
  2. import java.lang.reflect.Field;
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import org.junit.Test;
  10. import com.atguigu3.bean.Customer;
  11. import com.atguigu3.util.JDBCUtils;
  12. /**
  13. *
  14. * @Description 针对于Customers表的查询操作
  15. * @author shkstart Email:shkstart@126.com
  16. * @version
  17. * @date 上午10:04:55
  18. *
  19. */
  20. public class CustomerForQuery {
  21. @Test
  22. public void testQueryForCustomers(){
  23. String sql = "select id,name,birth,email from customers where id = ?";
  24. Customer customer = queryForCustomers(sql, 13);
  25. System.out.println(customer);
  26. sql = "select name,email from customers where name = ?";
  27. Customer customer1 = queryForCustomers(sql,"周杰伦");
  28. System.out.println(customer1);
  29. }
JDBC - PS - 查 - 封装ORM - 1sql1表多列1行