jdbc demo
JdbcUnits.java
1 package com.zto.branchaging.common.util; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 import java.util.Properties; 8 9 import org.apache.kylin.jdbc.Driver; 10 11 /** 12 * JDBC工具类 13 * 14 * @author KAKAXI 15 * 16 */ 17 public final class JdbcUnits { 18 19 private static Properties info; 20 21 private static Driver driver; 22 23 /** 24 * 装载驱动 25 */ 26 static { 27 28 try { 29 driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance(); 30 } catch (InstantiationException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } catch (IllegalAccessException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } catch (ClassNotFoundException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 info = new Properties(); 41 info.put("user", "ADMIN"); 42 info.put("password", "KYLIN"); 43 } 44 45 /** 46 * 建立数据库连接 47 * 48 * @return 49 * @throws SQLException 50 */ 51 public static Connection getConnection() throws SQLException { 52 Connection conn = driver.connect("jdbc:kylin://10.9.12.11:7070/SCAN_PROJECT", info); 53 return conn; 54 } 55 56 /** 57 * 释放连接 58 * @param conn 59 */ 60 private static void freeConnection(Connection conn) { 61 try { 62 conn.close(); 63 } catch (SQLException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 } 68 69 /** 70 * 释放statement 71 * @param statement 72 */ 73 private static void freeStatement(Statement statement) { 74 try { 75 statement.close(); 76 } catch (SQLException e) { 77 // TODO Auto-generated catch block 78 e.printStackTrace(); 79 } 80 } 81 82 /** 83 * 释放resultset 84 * @param rs 85 */ 86 private static void freeResultSet(ResultSet rs) { 87 try { 88 rs.close(); 89 } catch (SQLException e) { 90 // TODO Auto-generated catch block 91 e.printStackTrace(); 92 } 93 } 94 95 /** 96 * 释放资源 97 * 98 * @param conn 99 * @param statement 100 * @param rs 101 */ 102 public static void free(Connection conn, Statement statement, ResultSet rs) { 103 if (rs != null) { 104 freeResultSet(rs); 105 } 106 if (statement != null) { 107 freeStatement(statement); 108 } 109 if (conn != null) { 110 freeConnection(conn); 111 } 112 } 113 114 public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException { 115 Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance(); 116 Properties info = new Properties(); 117 info.put("user", "ADMIN"); 118 info.put("password", "KYLIN"); 119 Connection conn = driver.connect("jdbc:kylin://*.*.*.*:7070/SCAN_PROJECT", info); 120 121 Statement state = conn.createStatement(); 122 ResultSet resultSet = state.executeQuery("select line_id,weight from scan.zto_quantity_statis where rec_scan_date = '20160601' and REC_ PROV_ID = '310000' and sign_prov_id = '330000' and segment_weight > 1 and segment_weight <= 3"); 123 124 while (resultSet.next()) { 125 System.out.println(resultSet.getString(1) +" --- "+ resultSet.getString(2)); 126 } 127 System.out.println("aaaaa"); 128 } 129 }
JdbcHelper.java
1 package com.zto.branchaging.common.util; 2 import java.lang.reflect.Field; 3 import java.lang.reflect.Method; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.ResultSetMetaData; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 import java.util.ArrayList; 11 import java.util.Date; 12 import java.util.HashMap; 13 import java.util.List; 14 import java.util.Map; 15 16 /** 17 * 数据库访问帮助类 18 * 19 * @author KAKAXI 20 * 21 */ 22 public class JdbcHelper { 23 24 private static Connection conn = null; 25 private static PreparedStatement preparedStatement = null; 26 27 /** 28 * 用于查询,返回结果集 29 * 30 * @throws SQLException 31 */ 32 @SuppressWarnings("rawtypes") 33 public static List query(String sql) throws SQLException { 34 35 ResultSet rs = null; 36 try { 37 getPreparedStatement(sql); 38 rs = preparedStatement.executeQuery(); 39 return ResultToListMap(rs); 40 } catch (SQLException e) { 41 throw new SQLException(e); 42 } finally { 43 free(rs); 44 } 45 46 } 47 48 /** 49 * 用于带参数的查询,返回结果集 50 * 51 * @param sql 52 * sql语句 53 * @param paramters 54 * 参数集合 55 * @return 结果集 56 * @throws SQLException 57 */ 58 @SuppressWarnings("rawtypes") 59 public static ResultSet query(String sql, Object... paramters) 60 throws SQLException { 61 62 ResultSet rs = null; 63 try { 64 getPreparedStatement(sql); 65 66 //preparedStatement.setMaxRows(26);//关键代码,设置最大记录数为当前页记 67 68 for (int i = 0; i < paramters.length; i++) { 69 preparedStatement.setObject(i + 1, paramters[i]); 70 } 71 rs = preparedStatement.executeQuery(); 72 // rs.first(); 73 // rs.relative(1);//关键代码,直接移动游标为当前页起始记录处 74 75 return rs; 76 } catch (SQLException e) { 77 throw new SQLException(e); 78 } finally { 79 // free(rs); 80 } 81 } 82 83 84 public static <T> List<T> convertToList(ResultSet rs,Class<T> t) throws SQLException { 85 List result = new ArrayList(); 86 try { 87 result = obtainEntityObject(rs,t); 88 } catch (Exception e) { 89 e.printStackTrace(); 90 }finally{ 91 free(rs); 92 } 93 /*List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 94 ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData(); 95 int columnCount = md.getColumnCount(); 96 while (rs.next()) { 97 Map<String, Object> rowData = new HashMap<String, Object>(); 98 for (int i = 1; i <= columnCount; i++) { 99 rowData.put(md.getColumnName(i), rs.getObject(i)); 100 } 101 list.add(rowData); 102 } 103 String sss = list.toString(); 104 sss = sss.replace("]", "").replace("[", ""); 105 JSONArray jr = JSONArray.parseArray(sss); 106 //JSONArray jr = JSONArray.fromObject(list); 107 //List<T> resultList = JSONArray.toList(jr, t); 108 List<T> listDay = new ArrayList<T>(JSONArray.parseArray(jr.toJSONString(),t)); 109 return listDay; */ 110 return result; 111 } 112 /** 113 * 返回单个结果的值,如count\min\max等等 114 * 115 * @param sql 116 * sql语句 117 * @return 结果集 118 * @throws SQLException 119 */ 120 public static Object getSingle(String sql) throws SQLException { 121 Object result = null; 122 ResultSet rs = null; 123 try { 124 getPreparedStatement(sql); 125 rs = preparedStatement.executeQuery(); 126 if (rs.next()) { 127 result = rs.getObject(1); 128 } 129 return result; 130 } catch (SQLException e) { 131 throw new SQLException(e); 132 } finally { 133 free(rs); 134 } 135 136 } 137 138 @SuppressWarnings({ "unchecked", "rawtypes" }) 139 private static List ResultToListMap(ResultSet rs) throws SQLException { 140 List list = new ArrayList(); 141 while (rs.next()) { 142 ResultSetMetaData md = rs.getMetaData(); 143 Map map = new HashMap(); 144 for (int i = 1; i < md.getColumnCount(); i++) { 145 map.put(md.getColumnLabel(i), rs.getObject(i)); 146 } 147 list.add(map); 148 } 149 return list; 150 } 151 152 /** 153 * 获取PreparedStatement 154 * 155 * @param sql 156 * @throws SQLException 157 */ 158 private static void getPreparedStatement(String sql) throws SQLException { 159 conn = JdbcUnits.getConnection(); 160 preparedStatement = conn.prepareStatement(sql); 161 } 162 163 /** 164 * 释放资源 165 * 166 * @param rs 167 * 结果集 168 */ 169 public static void free(ResultSet rs) { 170 171 JdbcUnits.free(conn, preparedStatement, rs); 172 } 173 174 /** 175 * 释放资源 176 * 177 * @param statement 178 * @param rs 179 */ 180 public static void free(Statement statement, ResultSet rs) { 181 JdbcUnits.free(conn, statement, rs); 182 } 183 184 /** 185 * 释放资源 186 */ 187 public static void free() { 188 free(null); 189 } 190 191 //转化成实体list(注意实体类字段类型在此方法中必须要指定) 192 public static <T> List<T> obtainEntityObject(ResultSet rs, Class cls) throws Exception { 193 List<String> columnList = new ArrayList<String>(); 194 ResultSetMetaData data = rs.getMetaData(); 195 for (int i = 1; i <= data.getColumnCount(); i++) { 196 // 获得指定列的列名 197 String columnName = data.getColumnName(i); 198 columnList.add(columnName); 199 }; 200 List list = new ArrayList(); 201 Field[] fields = cls.getDeclaredFields(); 202 while (rs.next()) { 203 Object obj = newInstance(cls); 204 for(int i=0;i<columnList.size();i++){ 205 for (int j = 0; j < fields.length; j++) { 206 Class<?> type = fields[j].getType(); 207 String fieldName = fields[j].getName(); 208 String fieldNameUpper = fieldName.toUpperCase(); 209 if(fieldNameUpper.equals(columnList.get(i))){ 210 if (type.getName().equals(String.class.getName())) { 211 String value = rs.getString(fieldName); 212 setProperty(obj, fieldName, value); 213 break; 214 }else if (type.getName().equals(int.class.getName())||type.getName().equals(Integer.class.getName())) { 215 Integer value = rs.getInt(fieldName); 216 setProperty(obj, fieldName, value); 217 break; 218 }else if (type.getName().equals(double.class.getName())||type.getName().equals(Double.class.getName())) { 219 Double value = rs.getDouble(fieldName); 220 setProperty(obj, fieldName, value); 221 break; 222 }else if (type.getName().equals(long.class.getName())||type.getName().equals(Long.class.getName())) { 223 Long value = rs.getLong(fieldName); 224 setProperty(obj, fieldName, value); 225 break; 226 }else if (type.getName().equals(short.class.getName())||type.getName().equals(Short.class.getName())) { 227 Short value = rs.getShort(fieldName); 228 setProperty(obj, fieldName, value); 229 break; 230 }else if(type.getName().equals(Date.class.getName())) { 231 Date value = rs.getDate(fieldName); 232 setProperty(obj, fieldName, value); 233 break; 234 } 235 }; 236 237 } 238 } 239 list.add(obj); 240 } 241 return list; 242 243 } 244 245 private static Object newInstance(Class<?> cls) throws Exception { 246 return cls.newInstance(); 247 } 248 249 private static void setProperty(Object obj, String name, Object value) throws Exception { 250 Class<? extends Object> clazz = obj.getClass(); 251 String methodName = returnSetMethodName(name); 252 Method[] ms = clazz.getMethods(); 253 for (Method m : ms) { 254 if (m.getName().equals(methodName)) { 255 if (m.getParameterTypes().length == 1) { 256 Class<?> clazzParameterType = m.getParameterTypes()[0]; 257 setFieldValue(clazzParameterType.getName(), value, m, obj); 258 break; 259 260 } 261 } 262 } 263 } 264 265 private static void setFieldValue(String parameterTypeName, Object value, Method m, Object obj) throws Exception { 266 267 m.invoke(obj, value); 268 return; 269 /*if (parameterTypeName.equals(int.class.getName())) { 270 value = new Integer(Integer.parseInt(value.toString())); 271 m.invoke(obj, value); 272 return; 273 } 274 if (parameterTypeName.equals(String.class.getName())) { 275 m.invoke(obj, value); 276 return; 277 }*/ 278 } 279 280 private static String returnSetMethodName(String name) { 281 name = name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); 282 return "set" + name; 283 } 284 }
ztoQuantityStatisDaoImpl.java
package com.zto.branchaging.core.dao.impl; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.stereotype.Repository; import com.zto.branchaging.common.util.JdbcHelper; import com.zto.branchaging.core.dao.ZtoQuantityStatisDao; import com.zto.branchaging.dal.model.TimeLineAmount; @Repository public class ztoQuantityStatisDaoImpl implements ZtoQuantityStatisDao{ String sql = null; @Override public List<TimeLineAmount> selectProvToProvAmount(String paramDate,String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { sql = "select 0 as rangNo,'20160601'as dateTime,'666' as ddd, " + "line_id as lineId,weight as countWeigth from scan.zto_quantity_statis where rec_scan_date = ? and REC_PROV_ID = ? and sign_prov_id = '330000' and segment_weight > 1 and segment_weight <= 3"; sql = " SELECT Z.startSite,Z.endSite,Z.type,Z.COUNTONE as countOne,Z.COUNTWEIGTH,Z.BACKCOUNT,Z.BACKCOUNTWEIGTH,Z.percentage,X.countAllTotal,X.weigthAllTotal FROM (select L.startSite,L.endSite,L.type,L.COUNTONE,L.COUNTWEIGTH,M.COUNTONE as BACKCOUNT,M.COUNTWEIGTH AS BACKCOUNTWEIGTH,L.COUNTONE/M.COUNTONE as percentage from (select l.sign_prov_id as startSite, l.rec_prov_id as endSite,l.type,count(1) as COUNTONE,SUM(WEIGHT) AS COUNTWEIGTH from ZTO_QUANTITY_STATIS l where statistics_date=? group by l.sign_prov_id,l.rec_prov_id,l.type )L left join ( select sign_prov_id as startSite,rec_prov_id as endSite,type,COUNT(1) AS COUNTONE,SUM(WEIGHT) AS COUNTWEIGTH from ZTO_QUANTITY_STATIS where statistics_date='20160601' group by sign_prov_id,rec_prov_id,type)M on L.startSite = M.endSite and L.endSite = M.startSite and L.Type = M.type order by COUNTONE desc )Z, ( select COUNT(1) AS countAllTotal,SUM(WEIGHT) AS weigthAllTotal from ZTO_QUANTITY_STATIS l where statistics_date='20160601' and l.sign_prov_id = '310000' )X"; List<TimeLineAmount> result = new ArrayList<TimeLineAmount>(); System.out.println("进来了!!!!"); String timeOne = "20160601"; String two = "310000"; try { result = JdbcHelper.convertToList(JdbcHelper.query(sql, timeOne),TimeLineAmount.class); } catch (SQLException e) { System.out.println("kyLin查询报错!"); e.printStackTrace(); } return result; } @Override public List<TimeLineAmount> selectProvToCityAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToProvAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToCityAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToCenterAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToCityAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToProvAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToCityAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToCenterAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectProvToCenterAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToCenterAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToProvAmount(String paramDate, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectProvToProvAmountOne(Date time, Date endTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectProvToCityAmountOne(Date time, Date endTime, String startSite, String endSite, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectProvToCenterAmountOne(Date time, Date endTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToProvAmountOne(Date time, Date endTime, String startSite, String endSite, String startTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToCityAmountOne(Date time, Date endTime, String startSite, String endSite, String startTopProvince, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToCenterAmountOne(Date time, Date endTime, String startSite, String endSite, String startTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToCenterAmountOne(Date time, Date endTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToProvAmountOne(Date time, Date endTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToCityAmountOne(Date time, Date endTime, String startSite, String endSite, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToProvAmountOne(Date time, Date endTime, String startSite, String endSite, String startTopProvince, String startTopCity, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToCityAmountOne(Date time, Date endTime, String startSite, String endSite, String startTopProvince, String startTopCity, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToCenterAmountOne(Date time, Date endTime, String startSite, String endSite, String startTopProvince, String startTopCity, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectProvToProvAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectProvToCityAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectProvToCenterAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToProvAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String startTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToCityAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String startTopProvince, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCityToCenterAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String startTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToCenterAmountDetail( Date beginTime, Date overTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToProvAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectCenterToCityAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToProvAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String startTopProvince, String startTopCity, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToCityAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String startTopProvince, String startTopCity, String endTopProvince, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } @Override public List<TimeLineAmount> selectSiteToCenterAmountDetail(Date beginTime, Date overTime, String startSite, String endSite, String startTopProvince, String startTopCity, Integer fastType, Integer lastType, Integer type, String orderBy, String desc) { // TODO Auto-generated method stub return null; } // public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException { // Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance(); // Properties info = new Properties(); // info.put("user", "ADMIN"); // info.put("password", "KYLIN"); // Connection conn = driver.connect("jdbc:kylin://*.*.*.*:7070/SCAN_PROJECT", info); // // Statement state = conn.createStatement(); // ResultSet resultSet = state.executeQuery("select line_id,weight from scan.zto_quantity_statis where rec_scan_date = '20160601' and REC_PROV_ID = '310000' and sign_prov_id = '330000' and segment_weight > 1 and segment_weight <= 3"); // // while (resultSet.next()) { // System.out.println(resultSet.getString(1) +" --- "+ resultSet.getString(2)); // } // } }