|
| 1 | +package com.db.dbhelper; |
| 2 | + |
| 3 | +import java.io.Closeable; |
| 4 | +import java.io.IOException; |
| 5 | +import java.sql.Connection; |
| 6 | +import java.sql.DriverManager; |
| 7 | +import java.sql.ResultSet; |
| 8 | +import java.sql.ResultSetMetaData; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.sql.Statement; |
| 11 | +import java.sql.Types; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | + |
| 14 | +public class SqlDbHelper implements DataBaseHelper,Closeable { |
| 15 | + |
| 16 | + private String connString; |
| 17 | + |
| 18 | + private Connection getConnection() throws ClassNotFoundException, SQLException{ |
| 19 | + Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
| 20 | + return DriverManager.getConnection(connString); |
| 21 | + } |
| 22 | + |
| 23 | + private Statement getStatement() throws ClassNotFoundException, SQLException{ |
| 24 | + return getConnection().createStatement(); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public DataBaseHelper setConnectionString(String connString) { |
| 29 | + this.connString = connString; |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public int executeUpdate(String sqlQuery) throws ClassNotFoundException, SQLException { |
| 35 | + return getStatement().executeUpdate(sqlQuery); |
| 36 | + } |
| 37 | + |
| 38 | + //Outer map object will have data as <row no,MapObject>, inner map object will have the data as <Column Name,Column value> |
| 39 | + |
| 40 | + @Override |
| 41 | + public LinkedHashMap<Integer, LinkedHashMap<String, String>> executeQuery(String sqlQuery) throws ClassNotFoundException, SQLException { |
| 42 | + ResultSet result = getStatement().executeQuery(sqlQuery); |
| 43 | + |
| 44 | + String[] columnName = getColumnName(result); |
| 45 | + |
| 46 | + LinkedHashMap<Integer, LinkedHashMap<String, String>> dbData = new LinkedHashMap<>(); |
| 47 | + int couter = 1; // row no 1 |
| 48 | + while(result.next()){ |
| 49 | + dbData.put(couter, getDbData(columnName,result)); |
| 50 | + couter++; |
| 51 | + } |
| 52 | + return dbData; |
| 53 | + } |
| 54 | + |
| 55 | + private LinkedHashMap<String, String> getDbData(String[] columnName, ResultSet result) throws SQLException { |
| 56 | + LinkedHashMap<String, String> columnData = new LinkedHashMap<>(); |
| 57 | + |
| 58 | + for(int i = 0; i < columnName.length; i++){ |
| 59 | + columnData.put(columnName[i], getColumnData(i,result)); |
| 60 | + } |
| 61 | + |
| 62 | + return columnData; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + private String getColumnData(int i, ResultSet result) throws SQLException { |
| 67 | + int type = result.getMetaData().getColumnType(i + 1); |
| 68 | + |
| 69 | + switch (type) { |
| 70 | + case Types.VARCHAR: |
| 71 | + return result.getString(i + 1); |
| 72 | + case Types.NUMERIC: |
| 73 | + return result.getInt(i+1) + ""; |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + private String[] getColumnName(ResultSet result) throws SQLException { |
| 79 | + ResultSetMetaData data = result.getMetaData(); |
| 80 | + String[] columnName = new String[data.getColumnCount()]; |
| 81 | + |
| 82 | + for(int i = 1; i <= data.getColumnCount(); i++){ |
| 83 | + columnName[i-1] = data.getColumnName(i); |
| 84 | + } |
| 85 | + return columnName; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void close() throws IOException { |
| 90 | + // Logic for managing the connection |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +} |
0 commit comments