|
| 1 | +package com.chen.api.util.jdbc; |
| 2 | + |
| 3 | +import java.sql.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * 基本的jdbc查询数据库 |
| 7 | + * |
| 8 | + * @author : chen weijie |
| 9 | + * @Date: 2018-08-06 3:15 PM |
| 10 | + */ |
| 11 | +public class JDBCTest { |
| 12 | + |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + testQuery(); |
| 16 | + |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + /** |
| 21 | + * 查询操作 |
| 22 | + */ |
| 23 | + public static void testQuery() { |
| 24 | + |
| 25 | + Connection connection = getConnection(); |
| 26 | + |
| 27 | + try { |
| 28 | + String querySql = "select * from ent_user_cache_multi_provider_rule "; |
| 29 | + Statement statement = connection.createStatement(); |
| 30 | + ResultSet rs = statement.executeQuery(querySql); |
| 31 | + while (rs.next()) { |
| 32 | + System.out.println("rs:" + rs.getString("table_name")); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + } catch (SQLException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + releaseConnection(connection); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * 测试更新操作 |
| 44 | + */ |
| 45 | + public static void testUpdate() { |
| 46 | + Connection connection = getConnection(); |
| 47 | + |
| 48 | + String Sql = "update ent_user_cache_multi_provider_rule set salary=8000 where employee_id=100005"; |
| 49 | + try { |
| 50 | + Statement stmt1 = connection.createStatement();//或者用PreparedStatement方法 |
| 51 | + stmt1.executeUpdate(Sql);//执行sql语句 |
| 52 | + if (stmt1 != null) { |
| 53 | + try { |
| 54 | + stmt1.close(); |
| 55 | + } catch (SQLException e) { |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + } |
| 59 | + } catch (SQLException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * 测试事务 |
| 66 | + */ |
| 67 | + public static void testTraction() { |
| 68 | + |
| 69 | + /** |
| 70 | + * JDBC处理事务通过关闭连接的自动提交实现的: |
| 71 | +
|
| 72 | + Connection.setAutoCommit(false); |
| 73 | + 提交事务: |
| 74 | + Connection.commit(); |
| 75 | + 回滚事务 |
| 76 | + 回滚部分: |
| 77 | + Connection.rollback(Savepoint); |
| 78 | + 全部回滚: |
| 79 | + Connection.rollback(); |
| 80 | +
|
| 81 | + */ |
| 82 | + Connection connection = getConnection(); |
| 83 | + |
| 84 | + Statement statement = null; |
| 85 | + try { |
| 86 | + connection.setAutoCommit(false); |
| 87 | + statement = connection.createStatement(); |
| 88 | + |
| 89 | + String sql = "update employees set salary=500 where employee_id=100001"; |
| 90 | + statement.executeQuery(sql); |
| 91 | + sql = "update employees set salary=100 where employee_id=100002"; |
| 92 | + statement.executeQuery(sql); |
| 93 | + connection.commit(); |
| 94 | + } catch (SQLException e) { |
| 95 | + try { |
| 96 | + connection.rollback(); |
| 97 | + } catch (SQLException e1) { |
| 98 | + e1.printStackTrace(); |
| 99 | + } |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + |
| 103 | + if (statement != null) { |
| 104 | + try { |
| 105 | + statement.close(); |
| 106 | + } catch (SQLException e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + } |
| 110 | + releaseConnection(connection); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * 测试事务回滚 |
| 115 | + */ |
| 116 | + public static void testTractionRollBack() { |
| 117 | + |
| 118 | + Savepoint savepoint = null; |
| 119 | + Connection connection = getConnection(); |
| 120 | + |
| 121 | + Statement statement = null; |
| 122 | + try { |
| 123 | + connection.setAutoCommit(false); |
| 124 | + statement = connection.createStatement(); |
| 125 | + |
| 126 | + String sql = "update employees set salary=500 where employee_id=100001"; |
| 127 | + savepoint = connection.setSavepoint("savepoint1"); |
| 128 | + |
| 129 | + statement.executeQuery(sql); |
| 130 | + sql = "update employees set salary=100 where employee_id=100002"; |
| 131 | + statement.executeQuery(sql); |
| 132 | + connection.commit(); |
| 133 | + } catch (SQLException e) { |
| 134 | + e.printStackTrace(); |
| 135 | + try { |
| 136 | + if (savepoint == null) { |
| 137 | + // SQLException occurred in saving into Employee or Address |
| 138 | + // tables |
| 139 | + connection.rollback(); |
| 140 | + System.out.println("JDBC Transaction rolled back successfully"); |
| 141 | + } else { |
| 142 | + // exception occurred in inserting into Logs table |
| 143 | + // we can ignore it by rollback to the savepoint |
| 144 | + connection.rollback(savepoint); |
| 145 | + // lets commit now |
| 146 | + connection.commit(); |
| 147 | + } |
| 148 | + } catch (SQLException e1) { |
| 149 | + System.out.println("SQLException in rollback" + e.getMessage()); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if (statement != null) { |
| 154 | + try { |
| 155 | + statement.close(); |
| 156 | + } catch (SQLException e) { |
| 157 | + e.printStackTrace(); |
| 158 | + } |
| 159 | + } |
| 160 | + releaseConnection(connection); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + /** |
| 165 | + * 获取数据库连接 |
| 166 | + * |
| 167 | + * @return |
| 168 | + */ |
| 169 | + public static Connection getConnection() { |
| 170 | + |
| 171 | + Connection connection = null; |
| 172 | + //1.定义驱动程序名为driver内容为com.mysql.jdbc.Driver |
| 173 | + String driver = "com.mysql.jdbc.Driver"; |
| 174 | + //2.定义url;jdbc是协议;mysql是子协议:表示数据库系统管理名称;localhost:3306是你数据库来源的地址和目标端口;test是我本人建的表位置所在处,你以你的为标准。 |
| 175 | + String url = "jdbc:mysql://172.16.117.226:3306/ent_portal?useUnicode=true&characterEncoding=UTF8"; |
| 176 | + String user = "ent_all"; |
| 177 | + String password = "ent"; |
| 178 | + |
| 179 | + try { |
| 180 | + Class.forName(driver); |
| 181 | + //获取数据库连接,使用java.sql里面的DriverManager的getConnectin(String url , String username ,String password )来完成 |
| 182 | + connection = DriverManager.getConnection(url, user, password); |
| 183 | + |
| 184 | + //8.构造一个statement对象来执行sql语句:主要有Statement,PreparedStatement,CallableStatement三种实例来实现 |
| 185 | + //三种实现方法分别为: |
| 186 | + // Statement stmt = con.createStatement() ; |
| 187 | + //PreparedStatement pstmt = conn.prepareStatement(sql) ; |
| 188 | + // CallableStatement cstmt = conn.prepareCall("{CALL demoSp(? , ?)}") ; |
| 189 | + } catch (ClassNotFoundException e) { |
| 190 | + e.printStackTrace(); |
| 191 | + } catch (SQLException e) { |
| 192 | + e.printStackTrace(); |
| 193 | + } |
| 194 | + |
| 195 | + return connection; |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + /** |
| 200 | + * 释放数据库连接 |
| 201 | + * |
| 202 | + * @param conn |
| 203 | + */ |
| 204 | + public static void releaseConnection(Connection conn) { |
| 205 | + try { |
| 206 | + if (conn != null) |
| 207 | + conn.close(); |
| 208 | + } catch (Exception e) { |
| 209 | + e.printStackTrace(); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | +} |
0 commit comments