---恢复内容开始---
对于数据繁琐的各式各样的查询语句,每次都要写上一大段查询代码,不仅造成代码冗余,而且还浪费时间。下面给出自己写的一个数据库查询方法封装:
1 public class AllSelect {
2 public static List<Object> Select(String sql,String className) throws Exception{
3 //连接数据库
4 Connection conn = new MyConnection().getConnection();//后面有封装连接数据库的方法
5 //预处理
6 Statement st = conn.createStatement();
7 //执行sql语句,并把sql查询结果存储在resultSet中
8 ResultSet rs = st.executeQuery(sql);
9 //使用resultSetMetaDate获取ResultSet里面每条数据的字段名(数据库表里面的)
10 ResultSetMetaData rsmd = rs.getMetaData();
11 //查询结果一共有多少列,数据库表里面有多少个字段(属性)
12 int count = rsmd.getColumnCount();
13 //创建一个数组来存放结果集中所有的字段名(把每个字段存进数组里面)
14 String[] cols = new String[count];
15 //循环获取所有的字段名()
16 for(int i = 0;i < cols.length;i ++){
17 //把resultSetMetaDate获取的字段存进数组
18 cols[i] = rsmd.getColumnName(i+1);
19 }
20 //创建一个Arraylist存放解析出来的对象
21 List<Object> list = new ArrayList<Object>();
22 //获取类的反射,通过包名.类名。开始连接po层的类
23 Class clss = Class.forName(className);
24 while(rs.next()){
25 //每次通过反射创建一个对象
26 Object obj = clss.newInstance();
27 //通过反射获取对象所有的属性,
28 Field[] fie = clss.getDeclaredFields();
29 //遍历这个对象的所有属性,把数据库查询出来的数据放入类对象中
30 for(Field field:fie){
31 //判断·属性的类型,每种类型对应不同的获取属性方法
32 if(field.getType().getName().equals("java.lang.Integer")||
33 field.getType().getName().equals("int")){
34 //循环列名数组,找到属性名重名的列,获取这一列的值,给该属性赋值
35 for(int i = 0;i < cols.length;i ++){
36 //如果找到这一列
37 if(cols[i].equalsIgnoreCase(field.getName())){
38 //暴力访问
39 field.setAccessible(true);
40 //把表中查询出来的这一列的值给同名类的同名Int属性
41 field.set(obj, rs.getInt(cols[i]));
42 }
43 }
44 }else if(field.getType().getName().equalsIgnoreCase("java.lang.String")){
45 for(int i = 0;i < cols.length;i ++){
46 if(cols[i].equalsIgnoreCase(field.getName())){
47 //暴力访问
48 field.setAccessible(true);
49 //用这一列的值给同名的String属性
50 field.set(obj, rs.getString(cols[i]));
51 }
52 }
53 }else if(field.getType().getName().equalsIgnoreCase("java.sql.Date")){
54 for(int i = 0;i < cols.length;i ++){
55 if(cols[i].equalsIgnoreCase(field.getName())){
56 //暴力访问
57 field.setAccessible(true);
58 //用这一列的值给同名的Date属性
59 field.set(obj, rs.getDate(cols[i]));
60 }
61 }
62 }else if(field.getType().getName().equalsIgnoreCase("java.lang.Double")||
63 field.getType().getName().equalsIgnoreCase("double")){
64 //循环列名数组,找到属性名重名的列,获取这一列的值,给该属性赋值
65 for(int i = 0;i < cols.length;i ++){
66 //如果找到这一列
67 if(cols[i].equalsIgnoreCase(field.getName())){
68 //暴力访问
69 field.setAccessible(true);
70 //用这一列的值给同名的Double属性
71 field.set(obj, rs.getDouble(cols[i]));
72 }
73 }
74 }
75 }
76 list.add(obj);
77 }
78 rs.close();
79 st.close();
80 conn.close();
81 return list;
82 }
83 }
连接数据库的封装方法:
连接数据库接口:
1 public interface DBConnection {
2
3 public Connection getConnection();
4 public void close();
5 }
连接数据库实现类(Oracle数据库):
1 public class MyConnection implements DBConnection{
2 Connection conn;
3 @Override
4 public Connection getConnection() {
5 // TODO Auto-generated method stub
6 String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
7 String URL="jdbc:oracle:thin:@localhost:1521:benxi"; //benxi为数据库的SID
8 String Username="scott"; //用户名
9 String Password="123456"; //密码
10 try {
11 Class.forName(Driver) ;
12 conn=DriverManager.getConnection(URL,Username,Password);
13 } catch (ClassNotFoundException | SQLException e) {
14 // TODO Auto-generated catch block
15 e.printStackTrace();
16 } //加载数据库驱动
17
18 return conn;
19 }
20
21 @Override
22 public void close() {
23 // TODO Auto-generated method stub
24 try {
25 conn.close();
26 } catch (SQLException e) {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 }
30 }
31
32 }
连接数据库实现类(MySQL):
1 public class MysqlConnection implements DBConnection{
2 public static final String DRIVECLASS="com.mysql.jdbc.Driver";
3 public static final String URL="jdbc:mysql://localhost:3306/test01";
4 public static final String UNAME="root";
5 public static final String PASS="123456";
6 static{
7 try {
8 Class.forName(DRIVECLASS);
9 } catch (ClassNotFoundException e) {
10 // TODO Auto-generated catch block
11 e.printStackTrace();
12 }
13 }
14 /**
15 * 获得连接
16 */
17 Connection conn = null;
18 public Connection getConnection() {
19 try {
20 conn = DriverManager.getConnection(URL,UNAME,PASS);
21 } catch (SQLException e) {
22 e.printStackTrace();
23 }
24 return conn;
25 }
26 /**
27 * 关闭连接
28 */
29
30 @Override
31 public void close() {
32 // TODO Auto-generated method stub
33 try {
34 if(conn!=null&&!conn.isClosed()){
35 conn.close();
36 }
37 } catch (Exception e) {
38 e.printStackTrace();
39 }
40
41 }
42 }
---恢复内容结束---