oracle 连接数据库并查询,返回List<Map<String, Object>> 数据
package JDBC; import java.sql.Clob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class OracleJdbcTest { //数据库连接对象 private static Connection conn = null; private static String driver = "oracle.jdbc.OracleDriver";//"oracle.jdbc.driver.OracleDriver"; //驱动 private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; //连接字符串 private static String username = "******"; //用户名 private static String password = "******"; //密码 // 获得连接对象 private static synchronized Connection getConn(){ if(conn == null){ try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } } return conn; } //执行查询语句 public void query(String sql, boolean isSelect) throws SQLException{ PreparedStatement pstmt; try { pstmt = getConn().prepareStatement(sql); //建立一个结果集,用来保存查询出来的结果 ResultSet rs = pstmt.executeQuery(); while (rs.next()) { String name = rs.getString("bm_mc"); System.out.println(name); } rs.close(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } private static String clob2String(Clob clob) throws Exception { return (clob != null ? clob.getSubString(1, (int) clob.length()) : null); } private static List<Map<String, Object>> ResultSetToList(ResultSet rs) throws Exception { ResultSetMetaData md = rs.getMetaData(); // 得到结果集的结构信息,比如字段数、字段名等 // .getMetaData().getTableName(1) 就可以返回表名 int columnCount = md.getColumnCount(); // 得到结果集的列数 // System.out.println(columnCount); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> rowData; while (rs.next()) { //记录指针向下移动一个位置,如果其指向一条有效记录,则返回真;否则返回假。只有使记录指针不断移动,才能不断取出数据库中的数据 rowData = new HashMap<String, Object>(columnCount); for (int i = 1; i <= columnCount; i++) { Object v = rs.getObject(i); // 将任何数据类型返回为 Java Object if (v != null && (v.getClass() == Date.class || v.getClass() == java.sql.Date.class)) { // 反射 Timestamp ts = rs.getTimestamp(i); // 返回时间和日期 java.sql.Timestamp //rs.getDate()只是返回日期部分 java.sql.Date //rs.getTime()只是返回时间部分 java.sql.Time v = new java.util.Date(ts.getTime()); // v = ts; } else if (v != null && v.getClass() == Clob.class) { v = clob2String((Clob) v); // oracle11g 遇到取出来的字段是clob类型,clob用来存储大量文本数据 } //System.out.println("ResultSetToList:"+md.getColumnLabel(i)); // rowData.put(camelName(md.getColumnLabel(i)), v==null?"":v); rowData.put(md.getColumnLabel(i).toLowerCase(), v == null ? "" : v); //getColumnName(int column):获取指定列的名称 //getColumnLabel(int column):获取用于打印输出和显示的指定列的建议标题。 // toUpperCase 的意思是将所有的英文字符转换为大写字母 // toLowerCase的意思是将所有的英文字符转换为小写字母 } list.add(rowData); } return list; } //执行查询语句 public List<Map<String, Object>> queryList(String sql, boolean isSelect) throws Exception{ PreparedStatement pstmt = getConn().prepareStatement(sql);; List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); //建立一个结果集,用来保存查询出来的结果 ResultSet rs = pstmt.executeQuery(); list = ResultSetToList(rs); rs.close(); pstmt.close(); return list; } public void query(String sql) throws SQLException{ PreparedStatement pstmt; pstmt = getConn().prepareStatement(sql); pstmt.execute(); pstmt.close(); } //关闭连接 public void close(){ try { getConn().close(); } catch (SQLException e) { e.printStackTrace(); } } }
...
package JDBC; import java.sql.SQLException; import java.util.List; import java.util.Map; public class ConnOracle { public static void main(String[] args) throws Exception { OracleJdbcTest test = new OracleJdbcTest(); try { test.query("drop table student"); } catch (SQLException e) {} //test.query("create table student(id int, name nchar(20))"); //test.query("insert into student values(1,'zhangsan')"); //test.query("insert into student values(2,'lisi')"); //test.query("select r.id from T_ry r", true); String sql = "select r.* from T_RY r where r.data_flag <> 'D' "; List<Map<String, Object>> list = test.queryList(sql, true); System.out.println(list); test.close(); } }
时刻告诉自己,自己是个菜鸡......