您的位置:首页 > 数据库 > > 正文

如何看idea连接mysql数据库(IDEA 链接Mysql数据库并执行查询操作的完整代码)

更多 时间:2021-10-03 01:32:31 类别:数据库 浏览量:520

如何看idea连接mysql数据库

IDEA 链接Mysql数据库并执行查询操作的完整代码

 1、先写个 Mysql 的链接设置页面

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • package com.wretchant.fredis.menu.mysql;
  •  
  • import com.intellij.notification.NotificationType;
  • import com.intellij.openapi.actionSystem.AnAction;
  • import com.intellij.openapi.actionSystem.AnActionEvent;
  • import com.wretchant.fredis.gui.dialog.TableDialog;
  • import com.wretchant.fredis.util.NotifyUtils;
  • import com.wretchant.fredis.util.PropertiesUtils;
  • import org.jetbrains.annotations.NotNull;
  •  
  • import javax.swing.*;
  • import java.util.Map;
  • import java.util.Properties;
  •  
  • /**
  •  * @author Created by 谭健 on 2020/8/26. 星期三. 15:24.
  •  * © All Rights Reserved.
  •  */
  • public class MysqlConfig extends AnAction {
  •  
  •     @Override
  •     public void actionPerformed(@NotNull AnActionEvent event) {
  •  
  •         Properties properties = PropertiesUtils.readFromSystem();
  •         if (properties != null) {
  •             TableDialog.TableField build = TableDialog.TableField.build(properties.stringPropertyNames());
  •             TableDialog dialog = new TableDialog("Mysql 连接配置", build);
  •             for (int i = 0; i < dialog.getLabels().size(); i++) {
  •                 JLabel label = dialog.getLabels().get(i);
  •                 JTextField textField = dialog.getInputs().get(i);
  •                 String property = properties.getProperty(label.getText());
  •                 textField.setText(property);
  •             }
  •             dialog.show();
  •             if (dialog.isOK()) {
  •                 Map<String, String> valueMap = dialog.getValueMap();
  •                 valueMap.forEach(properties::setProperty);
  •                 PropertiesUtils.write2System(properties);
  •             }
  •         } else {
  •             NotifyUtils.notifyUser(event.getProject(), "读取配置文件失败,配置文件不存在", NotificationType.ERROR);
  •         }
  •     }
  •  
  • }
  • 如何看idea连接mysql数据库(IDEA 链接Mysql数据库并执行查询操作的完整代码)

    2、然后简单的写个 JDBC 操作数据库的支持类

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • package com.wretchant.fredis.support;
  •  
  • import cn.hutool.core.util.StrUtil;
  • import com.intellij.notification.NotificationType;
  • import com.intellij.openapi.actionSystem.AnActionEvent;
  • import com.intellij.openapi.actionSystem.PlatformDataKeys;
  • import com.intellij.openapi.editor.SelectionModel;
  • import com.wretchant.fredis.util.ClipboardUtils;
  • import com.wretchant.fredis.util.NotifyUtils;
  • import com.wretchant.fredis.util.PropertiesUtils;
  • import com.wretchant.fredis.value.StringValue;
  • import org.apache.commons.lang.StringUtils;
  • import org.jetbrains.annotations.NotNull;
  •  
  • import java.sql.*;
  • import java.util.*;
  •  
  • /**
  •  * @author Created by 谭健 on 2020/8/12. 星期三. 17:42.
  •  * © All Rights Reserved.
  •  */
  • public class Mysql {
  •  
  •  
  •     /**
  •      * 执行查询语句的返回结果
  •      */
  •     public static class Rs {
  •  
  •         public Rs(List<Map<String, Object>> r) {
  •             this.r = r;
  •             this.count = r.size();
  •         }
  •  
  •         private List<Map<String, Object>> r = new ArrayList<>();
  •  
  •         private int count;
  •  
  •         public List<Map<String, Object>> getR() {
  •             return r;
  •         }
  •  
  •         public void setR(List<Map<String, Object>> r) {
  •             this.r = r;
  •         }
  •  
  •         public int getCount() {
  •             return count;
  •         }
  •  
  •         public void setCount(int count) {
  •             this.count = count;
  •         }
  •  
  •         public Map<String, Object> one() {
  •             if (Objects.isNull(r) || r.isEmpty()) {
  •                 return null;
  •             }
  •             return r.get(0);
  •         }
  •  
  •  
  •         public Object oneGet(String key) {
  •             return one().get(key);
  •         }
  •     }
  •  
  •  
  •     // 参考: https://www.cnblogs.com/jyroy/p/9637149.html
  •  
  •     public static class JDBCUtil {
  •  
  •  
  •         /**
  •          * 执行sql 并返回 map 数据
  •          *
  •          * @param sql
  •          * @return
  •          */
  •         public static Rs rs(String sql) {
  •             Connection connection = null;
  •             Statement statement = null;
  •             ResultSet resultSet = null;
  •             List<Map<String, Object>> r = new ArrayList<>();
  •             try {
  •                 connection = Mysql.DatabaseUtils.getConnection();
  •                 statement = connection.createStatement();
  •                 resultSet = statement.executeQuery(sql);
  •  
  •                 // 基础信息
  •                 ResultSetMetaData metaData = resultSet.getMetaData();
  •                 // 返回了多少个字段
  •                 int columnCount = metaData.getColumnCount();
  •  
  •  
  •                 while (resultSet.next()) {
  •                     Map<String, Object> valueMap = new LinkedHashMap<>();
  •                     for (int i = 0; i < columnCount; i++) {
  •                         // 这个字段是什么数据类型
  •                         String columnClassName = metaData.getColumnClassName(i);
  •                         // 字段名称
  •                         String columnName = metaData.getColumnName(i);
  •                         Object value = resultSet.getObject(columnName);
  •                         valueMap.put(columnName, value);
  •                     }
  •                     r.add(valueMap);
  •                 }
  •             } catch (Exception e1) {
  •                 NotifyUtils.notifyUser(null, "error", NotificationType.ERROR);
  •                 e1.printStackTrace();
  •             } finally {
  •                 release(connection, statement, resultSet);
  •             }
  •             return new Rs(r);
  •         }
  •  
  •         public static ResultSet es(String sql) {
  •             Connection connection;
  •             Statement statement;
  •             ResultSet resultSet = null;
  •             try {
  •                 connection = Mysql.DatabaseUtils.getConnection();
  •                 statement = connection.createStatement();
  •                 resultSet = statement.executeQuery(sql);
  •             } catch (Exception e1) {
  •                 NotifyUtils.notifyUser(null, "error", NotificationType.ERROR);
  •                 e1.printStackTrace();
  •             }
  •             return resultSet;
  •         }
  •  
  •  
  •         public static void release(Connection connection, Statement st, ResultSet rs) {
  •             closeConn(connection);
  •             closeRs(rs);
  •             closeSt(st);
  •         }
  •  
  •         public static void closeRs(ResultSet rs) {
  •             try {
  •                 if (rs != null) {
  •                     rs.close();
  •                 }
  •             } catch (SQLException e) {
  •                 e.printStackTrace();
  •             } finally {
  •                 rs = null;
  •             }
  •         }
  •  
  •         private static void closeSt(Statement st) {
  •             try {
  •                 if (st != null) {
  •                     st.close();
  •                 }
  •             } catch (SQLException e) {
  •                 e.printStackTrace();
  •             } finally {
  •                 st = null;
  •            &n

    标签:mysql idea 查询
  • 上一篇:html5显示中心代码(HTML5中的Web Notification桌面通知功能的实现方法)
  • 下一篇:laravel 框架关键技术解析(在laravel框架中实现封装公共方法全局调用)
  • 您可能感兴趣