通过一个类将Mysql表映射到javabean
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过一个类将Mysql表映射到javabean相关的知识,希望对你有一定的参考价值。
DBHelper can retrieve mysql tables into java objects, without any external libraries or a mapping configuration file.note: please test before use, then use at your own risk.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; /** * @author asben14[at]outlook (dot) fr * @see for suggestions, comments * or donations here is my blog <a href="http://fivesnippets.blogspot.com/">fivesnippets.blogspot.com</a> */ public class DBHelper { /** * Initializes connection, can be replaced by DBHelper * constructor with some changes (make static variables attributes) in order * to have multiple connections instantly with many preparedStatement * objects */ public static void init() { try { Class.forName("com.mysql.jdbc.Driver"); e1.printStackTrace(); } try { e.printStackTrace(); } } public DBHelper() { // TODO Auto-generated constructor stub } /** * select rows from any table, provided that the table has the same columns as the bean * fields, similarity here is about names and types, types need to be * mapped correctly check the link bellow * @see <a href="http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html">Java, JDBC and MySQL Types</a> * @return Iterator of objects representing selected rows */ pst = con.prepareStatement("SELECT * FROM " + table + " WHERE " + condition); rs = pst.executeQuery(); rsmd = rs.getMetaData(); try { element = bean.getClass().newInstance(); } // the critical work is made here, these two loops are fetching data // from the resultSet object which contains rows, into the iterator // one row by one bean while (rs.next()) { rsmd = rs.getMetaData(); int length = element.getClass().getDeclaredFields().length; int length1 = rsmd.getColumnCount(); for (int i = 0; i < length1; i++) { for (int j = 0; j < length; j++) { if (element.getClass().getDeclaredFields()[j].getName() .equals(rsmd.getColumnName(i + 1))) { element.getClass().getDeclaredFields()[j].set(element, rs.getObject(i + 1)); } } } link.add(element); try { element = bean.getClass().newInstance(); } } return link.iterator(); } /** * @return a single row from any table and map it into a bean, * provided that it has the same columns as the bean fields, * similarity here is about names and types, types need to be mapped * correctly */ pst = con.prepareStatement("SELECT * FROM " + table + " WHERE " + condition); rs = pst.executeQuery(); rsmd = rs.getMetaData(); rs.next(); int length = bean.getClass().getDeclaredFields().length; int length1 = rsmd.getColumnCount(); if (rs.getRow() == 1) { for (int i = 0; i < length1; i++) { for (int j = 0; j < length; j++) { if (bean.getClass().getDeclaredFields()[j].getName() .equals(rsmd.getColumnName(i + 1))) { bean.getClass().getDeclaredFields()[j].set(bean, rs.getObject(i + 1)); } } } } else return null; return bean; } }
以上是关于通过一个类将Mysql表映射到javabean的主要内容,如果未能解决你的问题,请参考以下文章