00001 package edu.tum.cs.util.db; 00002 00003 import java.sql.Connection; 00004 import java.sql.ResultSet; 00005 import java.sql.ResultSetMetaData; 00006 import java.sql.SQLException; 00007 import java.sql.Statement; 00008 import java.util.HashMap; 00009 00014 public class DatabaseTool { 00023 public static HashMap<String,String> resultSetRow2HashMap(ResultSet rs) throws Exception { 00024 HashMap<String,String> hm = new HashMap<String,String>(); 00025 boolean erroneous = false; 00026 try { 00027 ResultSetMetaData rsmd = rs.getMetaData(); 00028 int numCols = rsmd.getColumnCount(); 00029 if(rs.next()) { 00030 for(int i = 1; i <= numCols; i++) { 00031 String val = rs.getString(i); 00032 if(val == null) 00033 erroneous = true; 00034 hm.put(rsmd.getColumnName(i), val); 00035 } 00036 } 00037 else 00038 return null; 00039 } 00040 catch(SQLException e) { 00041 e.printStackTrace(); 00042 } 00043 if(erroneous) 00044 throw new Exception("Result set contains null entry!"); 00045 return hm; 00046 } 00047 00055 public static String queryResult(Connection conn, String query) throws SQLException { 00056 Statement stmt = conn.createStatement(); 00057 ResultSet rs = stmt.executeQuery(query); 00058 rs.next(); 00059 return rs.getString(1); 00060 } 00061 }