public class ORMTest extends HttpServlet {
private static final long serialVersionUID = 1L;
<span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doGet</span>(HttpServletRequest request, HttpServletResponse response) <span class="hljs-keyword">throws</span> ServletException, IOException {</br>
<span class="hljs-comment">//创建sql语句</span></br>
String sql = <span class="hljs-string">"select username as Username,password as Password from user where id<?"</span>;</br>
<span class="hljs-comment">//创建个数组,用于给sql语句中的sql赋值</span></br>
Object obj[] = <span class="hljs-keyword">new</span> Object[]{<span class="hljs-number">1020</span>};</br>
<span class="hljs-keyword">try</span> {</br>
<span class="hljs-comment">//调用getObject方法得到一个Object对象的集合</span></br>
List<Object> list = getObject(sql, obj, User.class);</br>
<span class="hljs-comment">//将list存在request域中</span></br>
request.setAttribute(<span class="hljs-string">"list"</span>, list);</br>
<span class="hljs-comment">//转发到index.jsp页面</span></br>
request.getRequestDispatcher(<span class="hljs-string">"/index.jsp"</span>).forward(request, response);</br>
} <span class="hljs-keyword">catch</span> (InstantiationException | IllegalAccessException</br>
| IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();</br>
}</br></br>
}</br>
<span class="hljs-keyword">static</span> List<Object> getObject(String sql,Object obj[],Class clazz) <span class="hljs-keyword">throws</span> InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{</br>
Connection conn=<span class="hljs-keyword">null</span>;</br>
PreparedStatement ps = <span class="hljs-keyword">null</span>;</br>
ResultSet rs = <span class="hljs-keyword">null</span>;</br>
JDBCUtils utils = JDBCUtils.getInstance();</br>
List<Object> list = <span class="hljs-keyword">new</span> ArrayList<>();</br>
<span class="hljs-keyword">try</span> {</br>
conn = utils.getConnection();</br>
ps = conn.prepareStatement(sql);</br>
<span class="hljs-comment">//获取参数的元数据</span></br>
ParameterMetaData pmd = ps.getParameterMetaData();</br>
<span class="hljs-comment">//获取?个数</span></br>
<span class="hljs-keyword">int</span> count = pmd.getParameterCount();</br>
<span class="hljs-comment">//将数组中的元素对应的赋值给sql中的?</span></br>
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<=count;i++){</br>
ps.setObject(i, obj[i-<span class="hljs-number">1</span>]);</br>
}</br>
<span class="hljs-comment">//执行sql</span></br>
rs=ps.executeQuery();</br>
<span class="hljs-comment">//得到传进来的Bean的所有方法</span></br>
Method ms[] = clazz.getMethods();</br>
<span class="hljs-comment">//遍历结果集</span></br>
<span class="hljs-keyword">while</span>(rs.next()){</br>
<span class="hljs-comment">//得到结果集元数据</span></br>
ResultSetMetaData rsmd = rs.getMetaData();</br>
<span class="hljs-comment">//得到一个结果集的列数</span></br>
<span class="hljs-keyword">int</span> columnCount = rsmd.getColumnCount();</br>
<span class="hljs-comment">//得到bean的实例对象</span></br>
Object object = clazz.newInstance();</br>
<span class="hljs-comment">//遍历结果集的列</span></br>
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i<=columnCount;i++){</br>
String columnLabel =rsmd.getColumnLabel(i);<span class="hljs-comment">//得到列的别名</span></br>
<span class="hljs-comment">//得到别名后 可以得到这个列的bean的set方法</span></br>
String methodName = <span class="hljs-string">"set"</span>+columnLabel;</br>
<span class="hljs-comment">//遍历bean的所有方法</span></br>
<span class="hljs-keyword">for</span> (Method method : ms) {</br>
<span class="hljs-comment">//如果有和这个列的set方法相同的方法</span>
<span class="hljs-keyword">if</span>(method.getName().equals(methodName)){</br>
<span class="hljs-comment">//就将数据库查出来的这个列的值付给这个bean</span></br>
method.invoke(object, rs.getObject(columnLabel));</br>
}</br>
}</br>
}</br>
<span class="hljs-comment">//将这个bean添加到集合中</span>
list.add(object);
}
} <span class="hljs-keyword">catch</span> (SQLException e) {</br>
e.printStackTrace();</br>
}<span class="hljs-keyword">finally</span>{</br>
utils.free(conn, ps, rs);</br>
}</br>
<span class="hljs-keyword">return</span> list;</br>
}</br>
}
//bean
public class User {
public User() {
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> id;</br>
<span class="hljs-keyword">public</span> String username;</br>
<span class="hljs-keyword">public</span> String password;</br>
<span class="hljs-keyword">public</span> String sex;</br>
<span class="hljs-keyword">public</span> String age;</br>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getId</span>() {</br>
<span class="hljs-keyword">return</span> id;</br>
}</br>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setId</span>(<span class="hljs-keyword">int</span> id) {</br>
<span class="hljs-keyword">this</span>.id = id;</br>
}</br>
<span class="hljs-keyword">public</span> String <span class="hljs-title">getUsername</span>() {</br>
<span class="hljs-keyword">return</span> username;</br>
}</br>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUsername</span>(String username) {</br>
<span class="hljs-keyword">this</span>.username = username;</br>
}</br>
<span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() {</br>
<span class="hljs-keyword">return</span> password;</br>
}</br>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) {</br>
<span class="hljs-keyword">this</span>.password = password;</br>
}</br>
<span class="hljs-keyword">public</span> String <span class="hljs-title">getSex</span>() {</br>
<span class="hljs-keyword">return</span> sex;</br>
}</br>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setSex</span>(String sex) {</br>
<span class="hljs-keyword">this</span>.sex = sex;</br>
}</br>
<span class="hljs-keyword">public</span> String <span class="hljs-title">getAge</span>() {</br>
<span class="hljs-keyword">return</span> age;</br>
}</br>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAge</span>(String age) {</br>
<span class="hljs-keyword">this</span>.age = age;</br>
}</br></br>
<span class="hljs-annotation">@Override</span></br>
<span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span>() {</br>
<span class="hljs-keyword">return</span> <span class="hljs-string">"id="</span>+<span class="hljs-keyword">this</span>.id+<span class="hljs-string">"username="</span>+<span class="hljs-keyword">this</span>.username+<span class="hljs-string">"password="</span>+<span class="hljs-keyword">this</span>.password</br>
+<span class="hljs-string">"age="</span>+<span class="hljs-keyword">this</span>.age+<span class="hljs-string">"sex:"</span>+<span class="hljs-keyword">this</span>.sex;</br>
}</br></br>
}