package de.memtext.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Iterator; import java.util.LinkedList; import java.util.List; /** * @author MB */ public class DbUtils { /** * */ private DbUtils() { } /* * private Vector toResultVector(ResultSet rs) throws SQLException { Vector help * = new Vector(); ResultSetMetaData rsmd = rs.getMetaData(); int * numberOfColumns = rsmd.getColumnCount(); * * Vector row = null; while (rs.next()) { row = new Vector(); for (int i = 1; i * <= numberOfColumns; i++) { row.add(rs.getObject(i)); } help.add(row); } * rs.close(); return help; * * } */ public static List toResultList(ResultSet rs) throws SQLException { List help = new LinkedList(); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); List row = null; while (rs.next()) { row = new LinkedList(); for (int i = 1; i <= numberOfColumns; i++) { row.add(rs.getObject(i)); } help.add(row); } rs.close(); return help; } public static void grantRightToAllTables() { String url = "jdbc:hsqldb:hsql://localhost:9999"; String user = ""; String right = "select"; try { Class.forName("org.hsqldb.jdbcDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { DBAccess.addConnection("dbConn", DriverManager.getConnection(url, "admin", "hatschi3000")); } catch (SQLException e1) { e1.printStackTrace(); } catch (Exception e1) { e1.printStackTrace(); } List tablist = DBAccess.get("dbConn").getTableList(); StringBuffer buf = new StringBuffer(); for (Iterator it = tablist.iterator(); it.hasNext();) { String tabname = (String) it.next(); buf.append("grant " + right + " on " + tabname + " to " + user + ";"); } DBAccess.get("dbConn").execute(buf.toString()); DBAccess.closeConnection("dbConn"); } /** * Places Object in inverted single commas if it isn't null * * @param val * @return either "null" if val is null or "'val'" */ public static String placeInInvertedCommas(Object val) { if (val == null) return "null"; return "'" + val + "'"; } public static int getInt(Statement stm, String query) throws SQLException { int result = -1; ResultSet rs = stm.executeQuery(query); while (rs.next()) { result = rs.getInt(1); } rs.close(); return result; } public static String getString(Statement stm, String query) throws SQLException { String result = null; ResultSet rs = stm.executeQuery(query); while (rs.next()) { result = rs.getString(1); } rs.close(); return result; } /** * * @param query * @return -1 wenn nichts gefunden * @throws SQLException */ public static int getInt(Connection con, String query,int param) throws SQLException { int result = -1; PreparedStatement pst=con.prepareStatement(query); pst.setInt(1, param); ResultSet rs = pst.executeQuery(); while (rs.next()) { result = rs.getInt(1); } rs.close(); pst.close(); return result; } /** * * @param query * @return -1 wenn nichts gefunden * @throws SQLException */ public static int getInt(Connection con, String query,String param) throws SQLException { int result = -1; PreparedStatement pst=con.prepareStatement(query); pst.setString(1, param); ResultSet rs = pst.executeQuery(); while (rs.next()) { result = rs.getInt(1); } rs.close(); pst.close(); return result; } /** * * @param con * @param query * @param param * @return null if nothing found * @throws SQLException */ public static String getString(Connection con,String query,String param) throws SQLException { String result=null; PreparedStatement pst=con.prepareStatement(query); pst.setString(1, param); ResultSet rs = pst.executeQuery(); while (rs.next()) { result = rs.getString(1); } rs.close(); pst.close(); return result; } } //Created on 21.06.2003