| 74 | | //return a list of rows array->hashmap |
|---|
| | 75 | public boolean openAndCreateConnection(String databasepath){ |
|---|
| | 76 | connection = sqlServer.connectAndCreate(databasepath); |
|---|
| | 77 | |
|---|
| | 78 | if (connection == null) { |
|---|
| | 79 | logger.error("sql connection is null. database path="+databasepath); |
|---|
| | 80 | return false; |
|---|
| | 81 | } |
|---|
| | 82 | |
|---|
| | 83 | try{ |
|---|
| | 84 | connection.setAutoCommit(true); |
|---|
| | 85 | //by passing the two arguments, the ResultSet returned from executeQuery is updatable |
|---|
| | 86 | statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); |
|---|
| | 87 | if (statement == null) { |
|---|
| | 88 | logger.error("statement is null"); |
|---|
| | 89 | return false; |
|---|
| | 90 | } |
|---|
| | 91 | |
|---|
| | 92 | } |
|---|
| | 93 | catch(SQLException sqle){ |
|---|
| | 94 | logger.debug("Database Error occured when creating a statement object",sqle); |
|---|
| | 95 | return false; |
|---|
| | 96 | } |
|---|
| | 97 | return true; |
|---|
| | 98 | } |
|---|
| | 99 | |
|---|
| 102 | | |
|---|
| 103 | | /** check for the existence of a table**/ |
|---|
| 104 | | public boolean check4Table (String check_table) { |
|---|
| 105 | | try { |
|---|
| 106 | | statement.execute(check_table); |
|---|
| 107 | | } catch (SQLException sqle) { |
|---|
| 108 | | String theError = (sqle).getSQLState(); |
|---|
| 109 | | if (theError.equals("42X05")) // Table does not exist |
|---|
| 110 | | { |
|---|
| 111 | | return false; |
|---|
| 112 | | } |
|---|
| 113 | | else{ |
|---|
| 114 | | return true; |
|---|
| 115 | | } |
|---|
| 116 | | } |
|---|
| 117 | | // System.out.println("Just got the warning - table exists OK "); |
|---|
| 118 | | return true; |
|---|
| 119 | | } |
|---|
| | 131 | /** |
|---|
| | 132 | * Same as executeQuery except the return type is ResultSet |
|---|
| | 133 | * @param stat |
|---|
| | 134 | * @return ResultSet of querying the statement 'stat' |
|---|
| | 135 | */ |
|---|
| | 136 | public synchronized ResultSet queryResultSet(String stat){ |
|---|
| | 137 | ResultSet rs = null; |
|---|
| | 138 | try{ |
|---|
| | 139 | if (statement == null){ |
|---|
| | 140 | logger.info("Null sql statement provided."); |
|---|
| | 141 | return null; |
|---|
| | 142 | } |
|---|
| | 143 | logger.debug(stat); |
|---|
| | 144 | rs = statement.executeQuery(stat); |
|---|
| | 145 | |
|---|
| | 146 | logger.info("sql stat="+stat+ " result="+rs); |
|---|
| | 147 | } |
|---|
| | 148 | catch(SQLException sqle){ |
|---|
| | 149 | logger.info("Database Error occured when execute query " + stat, sqle); |
|---|
| | 150 | return null; |
|---|
| | 151 | } |
|---|
| | 152 | catch(Exception e){ |
|---|
| | 153 | logger.info("Exception="+e); |
|---|
| | 154 | return null; |
|---|
| | 155 | } |
|---|
| | 156 | logger.debug(" result="+rs); |
|---|
| | 157 | return rs; |
|---|
| | 158 | } |
|---|
| | 159 | /** |
|---|
| | 160 | * Used by create (table) |
|---|
| | 161 | */ |
|---|
| | 162 | public synchronized boolean execute(String stat) { |
|---|
| | 163 | boolean rs; |
|---|
| | 164 | try{ |
|---|
| | 165 | if (statement == null){ |
|---|
| | 166 | logger.info("statement is null."); |
|---|
| | 167 | return false; |
|---|
| | 168 | } |
|---|
| | 169 | |
|---|
| | 170 | rs = statement.execute(stat); |
|---|
| | 171 | // connection.commit(); |
|---|
| | 172 | } |
|---|
| | 173 | catch(SQLException sqle){ |
|---|
| | 174 | logger.debug("Database Error occured when execute query " + stat, sqle); |
|---|
| | 175 | return false; |
|---|
| | 176 | } |
|---|
| | 177 | catch(Exception e){ |
|---|
| | 178 | logger.debug(e); |
|---|
| | 179 | return false; |
|---|
| | 180 | } |
|---|
| | 181 | return rs; |
|---|
| | 182 | } |
|---|
| | 183 | /** |
|---|
| | 184 | * Used by insert, update, and delete |
|---|
| | 185 | */ |
|---|
| | 186 | public synchronized boolean executeUpdate(String stat) { |
|---|
| | 187 | int rs; |
|---|
| | 188 | try{ |
|---|
| | 189 | if (statement == null){ |
|---|
| | 190 | logger.info("statement is null."); |
|---|
| | 191 | return false; |
|---|
| | 192 | } |
|---|
| | 193 | |
|---|
| | 194 | //either the row count of INSERT, UPDATE OR DELETE statement, or 0 if the statement returns nothing |
|---|
| | 195 | rs = statement.executeUpdate(stat); |
|---|
| | 196 | // connection.commit(); |
|---|
| | 197 | logger.info("sql stat="+stat+ " result="+rs); |
|---|
| | 198 | } |
|---|
| | 199 | catch(SQLException sqle){ |
|---|
| | 200 | logger.info("Database Error occured when execute query " + stat, sqle); |
|---|
| | 201 | return false; |
|---|
| | 202 | } |
|---|
| | 203 | catch(Exception e){ |
|---|
| | 204 | logger.info("Exception="+e); |
|---|
| | 205 | return false; |
|---|
| | 206 | } |
|---|
| | 207 | logger.info(" result="+rs); |
|---|
| | 208 | return (rs==-1)? false : true; |
|---|
| | 209 | } |
|---|
| | 210 | /** |
|---|
| | 211 | * Used by checking the existence of table |
|---|
| | 212 | * Same as the method 'execute' except throws SQLException |
|---|
| | 213 | */ |
|---|
| | 214 | public void check4Table(String stat) throws SQLException { |
|---|
| | 215 | statement.executeQuery(stat); |
|---|
| | 216 | } |
|---|