1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos.devtools.datasources.databases.databasepool; 17 18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager; 19 import org.apache.logging.log4j.LogManager; 20 import org.apache.logging.log4j.Logger; 21 22 import java.sql.Connection; 23 import java.sql.PreparedStatement; 24 import java.sql.ResultSet; 25 import java.sql.SQLException; 26 import java.sql.Statement; 27 28 /** 29 * sql carried out 30 * 31 * @since 2021/10/26 32 */ 33 public class SqlRunnable { 34 private static final Logger LOGGER = LogManager.getLogger(SqlRunnable.class); 35 36 /** 37 * method 38 * 39 * @param conn Connection 40 * @param sql sql 41 * @return boolean 42 */ execute(Connection conn, String sql)43 public boolean execute(Connection conn, String sql) { 44 if (ProfilerLogManager.isInfoEnabled()) { 45 LOGGER.info("execute"); 46 } 47 boolean result = false; 48 Statement stmt = null; 49 try { 50 stmt = conn.createStatement(); 51 result = stmt.executeUpdate(sql) > 0 ? true : false; 52 } catch (SQLException throwAbles) { 53 if (ProfilerLogManager.isErrorEnabled()) { 54 LOGGER.error(throwAbles.getMessage()); 55 } 56 } finally { 57 close(stmt, conn); 58 } 59 return result; 60 } 61 62 /** 63 * executeBatch 64 * 65 * @param conn Connection 66 * @param ste Prepared Statement 67 * @return boolean 68 */ executeBatch(Connection conn, PreparedStatement ste)69 public boolean executeBatch(Connection conn, PreparedStatement ste) { 70 if (ProfilerLogManager.isInfoEnabled()) { 71 LOGGER.info("executeBatch"); 72 } 73 try { 74 conn.setAutoCommit(false); 75 int[] result = ste.executeBatch(); 76 conn.commit(); 77 return true; 78 } catch (SQLException throwAbles) { 79 try { 80 conn.rollback(); 81 } catch (SQLException exception) { 82 if (ProfilerLogManager.isErrorEnabled()) { 83 LOGGER.error(exception.getMessage()); 84 } 85 return false; 86 } 87 } finally { 88 close(ste, conn); 89 } 90 return false; 91 } 92 93 /** 94 * executeQuery 95 * 96 * @param stmt Statement 97 * @param sql sql 98 * @return ResultSet 99 */ executeQuery(Statement stmt, String sql)100 public ResultSet executeQuery(Statement stmt, String sql) { 101 if (ProfilerLogManager.isInfoEnabled()) { 102 LOGGER.info("executeQuery"); 103 } 104 ResultSet rs = null; 105 try { 106 rs = stmt.executeQuery(sql); 107 } catch (SQLException throwAbles) { 108 if (ProfilerLogManager.isErrorEnabled()) { 109 LOGGER.error(throwAbles.getMessage()); 110 } 111 } 112 return rs; 113 } 114 115 /** 116 * close 117 * 118 * @param st Statement 119 */ close(Statement st)120 public void close(Statement st) { 121 if (ProfilerLogManager.isInfoEnabled()) { 122 LOGGER.info("close"); 123 } 124 close(st, null, null); 125 } 126 127 /** 128 * close 129 * 130 * @param rs Result set 131 */ close(ResultSet rs)132 public void close(ResultSet rs) { 133 if (ProfilerLogManager.isInfoEnabled()) { 134 LOGGER.info("close"); 135 } 136 close(null, rs, null); 137 } 138 139 /** 140 * close 141 * 142 * @param con Connection 143 */ close(Connection con)144 public void close(Connection con) { 145 if (ProfilerLogManager.isInfoEnabled()) { 146 LOGGER.info("close"); 147 } 148 close(null, null, con); 149 } 150 151 /** 152 * close 153 * 154 * @param st Statement 155 * @param con Connection 156 */ close(Statement st, Connection con)157 public void close(Statement st, Connection con) { 158 if (ProfilerLogManager.isInfoEnabled()) { 159 LOGGER.info("close"); 160 } 161 close(st, null, con); 162 } 163 164 /** 165 * close 166 * 167 * @param rs Result set 168 * @param con Connection 169 */ close(ResultSet rs, Connection con)170 public void close(ResultSet rs, Connection con) { 171 if (ProfilerLogManager.isInfoEnabled()) { 172 LOGGER.info("close"); 173 } 174 close(null, rs, con); 175 } 176 177 /** 178 * close 179 * 180 * @param st Statement 181 * @param rs Result set 182 * @param con Connection 183 */ close(Statement st, ResultSet rs, Connection con)184 public void close(Statement st, ResultSet rs, Connection con) { 185 if (ProfilerLogManager.isInfoEnabled()) { 186 LOGGER.info("close"); 187 } 188 if (st != null) { 189 try { 190 st.close(); 191 } catch (SQLException exception) { 192 if (ProfilerLogManager.isErrorEnabled()) { 193 LOGGER.error(exception.getMessage()); 194 } 195 } 196 } 197 if (rs != null) { 198 try { 199 rs.close(); 200 } catch (SQLException exception) { 201 if (ProfilerLogManager.isErrorEnabled()) { 202 LOGGER.error(exception.getMessage()); 203 } 204 } 205 } 206 if (con != null) { 207 try { 208 con.close(); 209 } catch (SQLException exception) { 210 if (ProfilerLogManager.isErrorEnabled()) { 211 LOGGER.error(exception.getMessage()); 212 } 213 } 214 } 215 } 216 } 217