| 4 | |
| 5 | |
| 6 | final class RunSqlScript { |
| 7 | |
| 8 | public static String DBDRIVER = "com.mysql.cj.jdbc.Driver"; |
| 9 | public static String DBURL ; |
| 10 | //现在使用的是mysql数据库,是直接连接的,所以此处必须有用户名和密码 |
| 11 | public static String USERNAME ; |
| 12 | public static String PASSWORD ; |
| 13 | |
| 14 | public static Integer RunSelectCount(String sql) { |
| 15 | Integer count = 0; |
| 16 | //数据库连接对象 |
| 17 | Connection conn = null; |
| 18 | //数据库操作对象 |
| 19 | Statement stmt = null; |
| 20 | //1、加载驱动程序 |
| 21 | try { |
| 22 | Class.forName(DBDRIVER); |
| 23 | } catch (ClassNotFoundException e) { |
| 24 | e.printStackTrace(); |
| 25 | } |
| 26 | //2、连接数据库 |
| 27 | //通过连接管理器连接数据库 |
| 28 | try { |
| 29 | //在连接的时候直接输入用户名和密码才可以连接 |
| 30 | conn = DriverManager.getConnection(DBURL,USERNAME,PASSWORD); |
| 31 | } catch (SQLException e) { |
| 32 | e.printStackTrace(); |
| 33 | } |
| 34 | //3、向数据库中插入一条数据 |
| 35 | //String sql = "select count(*) from information_schema.columns where table_name = 'config' and column_name = 'id'"; |
| 36 | try { |
| 37 | stmt = conn.createStatement(); |
| 38 | } catch (SQLException e) { |
| 39 | e.printStackTrace(); |
| 40 | } |
| 41 | //4、执行语句 |
| 42 | try { |
| 43 | //stmt.executeUpdate(sql); |
| 44 | ResultSet resultSet = stmt.executeQuery(sql); |
| 45 | if(resultSet.next()){ |
| 46 | count = resultSet.getInt(1); |
| 47 | } |
| 48 | } catch (SQLException e) { |
| 49 | e.printStackTrace(); |
| 50 | return -1; |
| 51 | } |
| 52 | //5、关闭操作 |
| 53 | try { |
| 54 | stmt.close(); |
| 55 | conn.close(); |
| 56 | } catch (SQLException e) { |
| 57 | e.printStackTrace(); |
| 58 | } |
| 59 | return count; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | //insert操作 |
nothing calls this directly
no outgoing calls
no test coverage detected