java连接 oracle 遍历ResultSet,结果集耗尽

java连接oracle数据库

Connection conn = null; 
Statement stmt = null;
ResultSet rs = null;
String url="jdbc:oracle:thin:@192.239.99.109:1521:orcl";

	      try {
	            Class.forName("oracle.jdbc.driver.OracleDriver");
	            conn = DriverManager.getConnection(url, "g4studio", "g4studio");
	            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
	            		ResultSet.CONCUR_UPDATABLE);
	            rs = stmt.executeQuery("SELECT * FROM GEO_ENTITY_RECOG where STMC like '%"+searchName+"%'");
	            ResultSetMetaData m=rs.getMetaData();
	            int columns=m.getColumnCount();
	            rs.beforeFirst();//将结果集指针指回到开始位置
	            while(rs.next())
	            {
	             for(int i=1;i<=columns;i++)
	             {
	              System.out.print(rs.getString(i));
	              System.out.print("\t\t");
	             }
	            }
	        }
	         catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } 
	      finally {
	            try {
	                if(rs != null) {
	                    rs.close();
	                    rs = null;
	                }
	                if(stmt != null) {
	                    stmt.close();
	                    stmt= null;
	                }
	                if(conn != null) {
	                    conn.close();
	                    conn = null;
	                }
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
这样遍历了整个ResultSet,,oracle的遍历是从游标的头开始的,所以,首先得把游标放到第一位,要不会结果集耗尽

这样的sql有sql注入风险,练习之用.

你可能感兴趣的:(java连接 oracle 遍历ResultSet,结果集耗尽)