关闭

处理JDBC返回值类-JdbcExecute.java(请看上一篇)

标签: connectionjava
476人阅读 评论(0) 收藏 举报
分类:
package ht.chart.fusioncharts.dao;

import ht.chart.utils.ConvertUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;


public class JdbcExecute {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String sql = "SELECT   MAX(CASE WHEN a.COMMONDATAATTRCODE = 'departname' THEN B.COMMONDATAVALUE ELSE '' END) as departname, MAX( CASE WHEN a.COMMONDATAATTRCODE = 'count' THEN B.COMMONDATAVALUE ELSE '' END) as count,B.COMMONDATAVALUENO FROM      COMMONDATAATTR a JOIN COMMONDATAVALUE b ON A.COMMONDATAATTRID = B.COMMONDATAATTRID group by B.COMMONDATAVALUENO";
		JdbcExecute exec = new JdbcExecute();
		List<Map> lst = exec.getResultList(sql);
		for(Map map : lst)
		{
			System.out.println  (map.get("DEPARTNAME"));
		}
		System.out.println(lst);
	}

	public List<Map> getResultList(String sqlText) {
		Statement st = null;
		ResultSet rs = null;
		Connection conn = null;
		List rlist = null;
		try {
			conn = JdbcConnecton.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery(sqlText);
			rlist = ConvertUtil.rs2ListMap(rs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcConnecton.close(conn, st, rs);
		}
		return rlist;
	}
	
	public Map getResultMap (String sqlText) throws SQLException{
		Statement st = null;
		ResultSet rs = null;
		Connection conn = null;
		Map map = null;
		try {			
			conn = JdbcConnecton.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery(sqlText);
			map = ConvertUtil.rs2Map(rs);
		    } catch (ClassNotFoundException e){
			e.printStackTrace();
		    }finally {
			JdbcConnecton.close(conn, st, rs);
		    }
		return map;
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:38902次
    • 积分:871
    • 等级:
    • 排名:千里之外
    • 原创:47篇
    • 转载:7篇
    • 译文:0篇
    • 评论:2条
    最新评论