电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

IDEA 鏈接Mysql數據庫并執行查詢操作的完整代碼

瀏覽:70日期:2023-10-18 08:41:51

1、先寫個 Mysql 的鏈接設置頁面

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數據庫并執行查詢操作的完整代碼

2、然后簡單的寫個 JDBC 操作數據庫的支持類

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; }}private static void closeConn(Connection connection) { try {if (connection != null) { connection.close();} } catch (SQLException e) {e.printStackTrace(); } finally {connection = null; }} } public static class DatabaseUtils {private static Connection connection = null;static { Properties properties = PropertiesUtils.readFromSystem(); try {if (properties != null) { Class.forName('com.mysql.cj.jdbc.Driver'); connection = DriverManager.getConnection( properties.getProperty('mysql.url'), properties.getProperty('mysql.username'), properties.getProperty('mysql.password') ); NotifyUtils.notifyUser(null, '數據庫連接成功', NotificationType.INFORMATION);} } catch (Exception e) {NotifyUtils.notifyUser(null, '數據庫連接失敗', NotificationType.ERROR);e.printStackTrace(); }}public static Connection getConnection() { return connection;} } public static void exec(@NotNull AnActionEvent event, Template template) {StringValue stringValue = new StringValue(template.getDefaultValue());Optional.ofNullable(event.getData(PlatformDataKeys.EDITOR)).ifPresent(editor -> { SelectionModel selectionModel = editor.getSelectionModel(); String selectedText = selectionModel.getSelectedText(); if (StringUtils.isNotBlank(selectedText)) {stringValue.setValue(StrUtil.format(template.getDynamicValue(), selectedText)); }});ClipboardUtils.clipboard(stringValue.getValue());NotifyUtils.notifyUser(event.getProject(), stringValue.getValue(), NotificationType.INFORMATION); } /** * sql 語句模版 */ public enum Template {SELECT('SELECT * FROM x WHERE 1 = 1 AND ', 'SELECT * FROM {} WHERE 1 = 1 AND ', '查詢語句'),UPDATE('UPDATE x SET x = x WHERE 1 = 1 AND ', 'UPDATE {} SET x = x WHERE 1 = 1 AND ', '更新語句'),DELETE('DELETE FROM x WHERE 1 = 1 ', 'DELETE FROM {} WHERE 1 = 1 ', '刪除語句'),INSERT('INSERT INTO * (x) VALUES (x) ', 'INSERT INTO {} (x) VALUES (x) ', '新增語句'),;Template(String defaultValue, String dynamicValue, String describe) { this.defaultValue = defaultValue; this.dynamicValue = dynamicValue; this.describe = describe;}public String getDynamicValue() { return dynamicValue;}public String getDefaultValue() { return defaultValue;}public String getDescribe() { return describe;}/** * 模版內容:默認值 */private final String defaultValue;/** * 動態內容 */private final String dynamicValue;/** * 內容描述 */private final String describe; }}

3、寫個測試連接的類&#xff0c;測試一下 mysql 是否可以正常鏈接

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.support.Mysql;import com.wretchant.fredis.util.NotifyUtils;import org.jetbrains.annotations.NotNull;import java.sql.ResultSet;/** * @author Created by 譚健 on 2020/9/15. 星期二. 10:17. * © All Rights Reserved. */public class MysqlConn extends AnAction { @Override public void actionPerformed(@NotNull AnActionEvent event) {try { ResultSet es = Mysql.JDBCUtil.es('select 1 as ct'); es.next(); int ct = es.getInt('ct'); if (ct == 1) {NotifyUtils.notifyUser(null, '連接是正常的', NotificationType.INFORMATION); } else {NotifyUtils.notifyUser(null, '連接不正常', NotificationType.ERROR); } Mysql.JDBCUtil.closeRs(es);} catch (Exception e1) { e1.printStackTrace(); NotifyUtils.notifyUser(null, '連接不正常', NotificationType.ERROR);} }}

IDEA 鏈接Mysql數據庫并執行查詢操作的完整代碼

以上就是IDEA 鏈接Mysql數據庫并執行查詢操作的完整代碼的詳細內容,更多關于IDEA 鏈接Mysql執行查詢操作 的資料請關注好吧啦網其它相關文章!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 空压机网_《压缩机》杂志 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 户外-组合-幼儿园-不锈钢-儿童-滑滑梯-床-玩具-淘气堡-厂家-价格 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | 暖气片十大品牌厂家_铜铝复合暖气片厂家_暖气片什么牌子好_欣鑫达散热器 | 玖容气动液压设备有限公司-气液增压缸_压力机_增压机_铆接机_增压器 | _网名词典_网名大全_qq网名_情侣网名_个性网名 | 企小优-企业数字化转型服务商_网络推广_网络推广公司 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 菏泽商标注册_菏泽版权登记_商标申请代理_菏泽商标注册去哪里 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 振动传感器,检波器-威海广达勘探仪器有限公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 大连海岛旅游网>>大连旅游,大连海岛游,旅游景点攻略,海岛旅游官网 | 金现代信息产业股份有限公司--数字化解决方案供应商 | 「银杏树」银杏树行情价格_银杏树种植_山东程锦园林 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 金属回收_废铜废铁回收_边角料回收_废不锈钢回收_废旧电缆线回收-广东益夫金属回收公司 | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司 | 航空铝型材,7系铝型材挤压,硬质阳*氧化-余润铝制品 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 数显水浴恒温振荡器-分液漏斗萃取振荡器-常州市凯航仪器有限公司 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 包装机_厂家_价格-山东包装机有限公司 | 博客-悦享汽车品质生活| 「安徽双凯」自动售货机-无人售货机-成人用品-自动饮料食品零食售货机 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 珠宝展柜-玻璃精品展柜-首饰珠宝展示柜定制-鸿钛展柜厂家 | 河南道路标志牌_交通路标牌_交通标志牌厂家-郑州路畅交通 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 报警器_家用防盗报警器_烟雾报警器_燃气报警器_防盗报警系统厂家-深圳市刻锐智能科技有限公司 | 开业庆典_舞龙舞狮_乔迁奠基仪式_开工仪式-神挚龙狮鼓乐文化传媒 | 安徽免检低氮锅炉_合肥燃油锅炉_安徽蒸汽发生器_合肥燃气锅炉-合肥扬诺锅炉有限公司 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 |