创建全能的表格管理程序

		String sql = "SELECT * FROM \""+tableName+"\" LIMIT 0 OFFSET 0";
		List list = new ArrayList();
		Connection conn = null;
		try {
			conn = getConn();
			Statement st = null;
			try{
				st = conn.createStatement();
				ResultSet rs = null;
				try{
					rs = st.executeQuery(sql);
					ResultSetMetaData rsmd = rs.getMetaData();
					int cnt = rsmd.getColumnCount();
					for (int i = 1; i <= cnt; i++) {
						String type = rsmd.getColumnTypeName(i);
						if(type.equals("varchar") || type.equals("int4") || type.equals("bool") || type.equals("timestamp")){
							HashMap hashMap = new HashMap();
							hashMap.put("name", rsmd.getColumnName(i));
							hashMap.put("type", rsmd.getColumnTypeName(i));
							list.add(hashMap);
						}
					}
				}finally{
					if(rs!=null)rs.close();
				}
			}finally{
				if(st!=null)st.close();
			}
		} catch (NamingException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		} catch (SQLException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				}
		}
		return list;

你可能感兴趣的:(sql)