Database Operation

// condition detecting the presence or absence of data in accordance with
public sttaic int isdata (Connection conn, String sql) {

  PreparedStatement ps =null;

  ResultSet rs =null;

  try{

    ps =conn.perpareStatement(sql);

    rs = ps.executeQuery();

    while(rs.next()){

    return 1;

    }

    return 0;

  }catch(){

  }
  
}

//Query data

public List<Map<String,Object>> search(String sql){
Connection conn = getConnection();
PreparedStatement ps = null;
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int colomn = md.getColumnCount();
Map<String,Object> temp = null;
while(rs.next()) {
temp = new HashMap<String,Object>();
md = rs.getMetaData();
for(int i = 1; i <= colomn; i++) {
temp.put(md.getColumnName(i), rs.getObject(i));
}
list.add(temp);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
closeConnection(conn);
}
return list;

ResultSetMetaData There are two ways to get the field name

A, getColumnName (int index), this method is to obtain the field name in the table.

Two, getConlumnLabel (int index), obtained by this method is the name of the field you requested in the statement

 

Guess you like

Origin www.cnblogs.com/wangrongchen/p/11359328.html