| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.ObjectInputStream; |
| | | import java.io.ObjectOutputStream; |
| | | import java.sql.*; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | |
| | | /********************************* 核心层 *********************************/ |
| | | /*****************************************************************************/ |
| | | |
| | | private List<Map<String, Object>> executeQuery(String sql) { |
| | | |
| | | private synchronized List<Map<String, Object>> executeQuery(String sql) { |
| | | try { |
| | | Connection conn = getConn(); |
| | | pstm = conn.prepareStatement(sql); |
| | | rs = pstm.executeQuery(); |
| | | return convertList(rs); |
| | | List<Map<String, Object>> maps = convertList(rs); |
| | | return deepClone(maps); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | // if (conn != null) { |
| | | // try { |
| | | // conn.close(); |
| | | // } catch (SQLException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | if (conn != null) { |
| | | try { |
| | | conn.close(); |
| | | conn = null; |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 深拷贝 |
| | | */ |
| | | public ArrayList deepClone(List list) throws Exception { |
| | | // 序列化 |
| | | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| | | ObjectOutputStream oos = new ObjectOutputStream(bos); |
| | | |
| | | oos.writeObject(list); |
| | | |
| | | // 反序列化 |
| | | ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
| | | ObjectInputStream ois = new ObjectInputStream(bis); |
| | | |
| | | return (ArrayList) ois.readObject(); |
| | | } |
| | | |
| | | } |