database utility code example

Example 1: database utility

I have a 
1-create connection method
2-runQuery- to accept sql query
3-destroy method- for clean up all the connection statement etc
4-getRowCount
5-getColumnCount
6-getColumnNames
7-getRowDataAsList
8-getColumnDataAtRow
9-getColumnDataAsList
10-displayAllData
11-getRowMap
12-getAllDataAslistOfMap

Example 2: database utility class

public class DBUtility {
     private final static String url = ConfigReader.getProperty("JDBC_URL"),
     private final static String username =ConfigReader.getProperty("JDBC_UserName"),
     private final static String password = ConfigReader.getProperty("JDBC_PassWord");

        private static Connection connection;
        private static Statement statement;
        public static DatabaseMetaData metadata;

        static {    ==> it is a good static block and try/catch example...
            try {   
               connection = DriverManager.getConnection(url, username, password); 
                statement = connection.createStatement();
                metadata = connection.getMetaData();
            } catch(Exception e) {}
        }

        public static ResultSet getResult(String query) {
            ResultSet result = null;
            try {
                result = statement.executeQuery(query);
            } catch(Exception e) {}
            return result;
        }

        public static void tearDown() {
            try {
                connection.close();
            }catch(Exception e) { }

        }

Tags:

Misc Example