您的位置:首页 > 数据库

如何动态的获取数据库里面的字段以…

2017-12-07 22:46 134 查看
private String getResult(String sql){

  

  Connection conn=null;

  ResultSet rs=null;

  conn=
DBConnection.getConnection(Constants.DATABASE_PATH_STYLE);

  PreparedStatement selectPS =
conn.prepareStatement(sql);

  try {

    rs =
selectPS.executeQuery();

  } catch (SQLException e1)
{

   // TODO
Auto-generated catch block

   e1.printStackTrace();

  }

  ArrayList lstThemes=new
ArrayList();

  try {

   if(rs!=null){

    int
colCount=rs.getMetaData().getColumnCount();

    while(rs.next()){

     HashMap
hm=new HashMap();

     for(int
i=1;i<=rs.getMetaData().getColumnCount();i++){

      String
colName=rs.getMetaData().getColumnName(i);

      if("getValidColumnIndex".equals(colName))

      {

       continue;

      }

      hm.put(colName,rs.getString(colName));

     }

     lstThemes.add(hm);

    }

   }

  } catch (SQLException e)
{

   // TODO
Auto-generated catch block

   e.printStackTrace();

  }finally{

   DBConnection.closeConnection(conn);

  }

  JSONArray json =
JSONArray.fromObject(lstThemes);

  return json.toString();

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐