好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > IDEA 鏈接Mysql數據庫并執行查詢操作的完整代碼

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

熱門標簽:大豐地圖標注app 南太平洋地圖標注 武漢電銷機器人電話 html地圖標注并導航 催天下外呼系統 呂梁外呼系統 北京金倫外呼系統 400電話變更申請 400電話辦理服務價格最實惠

 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()) {
                MapString, String> valueMap = dialog.getValueMap();
                valueMap.forEach(properties::setProperty);
                PropertiesUtils.write2System(properties);
            }
        } else {
            NotifyUtils.notifyUser(event.getProject(), "讀取配置文件失敗,配置文件不存在", NotificationType.ERROR);
        }
    }

}

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(ListMapString, Object>> r) {
            this.r = r;
            this.count = r.size();
        }

        private ListMapString, Object>> r = new ArrayList>();

        private int count;

        public ListMapString, Object>> getR() {
            return r;
        }

        public void setR(ListMapString, Object>> r) {
            this.r = r;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public MapString, 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;
            ListMapString, 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()) {
                    MapString, 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執行查詢操作 的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • 使用Intellij IDEA查看Java源碼技巧
  • IntelliJ IDEA 小技巧之Bookmark(書簽)的使用
  • 詳解IDEA 中使用Maven創建項目常見錯誤和使用技巧(推薦)
  • IntelliJ IDEA插件EasyCode安裝方法與使用技巧
  • IntellJ IDEA神器使用技巧(小結)
  • 利用Intellij Idea連接遠程服務器實現遠程上傳部署功能
  • 快速使用IDEA圖形化界面連接Phoenix的方法
  • IDEA 如何控制編輯左側的功能圖標ICON(操作步驟)
  • 強烈推薦這些提升代碼效率的IDEA使用技巧

標簽:徐州 西寧 南充 無錫 自貢 龍巖 迪慶 麗水

巨人網絡通訊聲明:本文標題《IDEA 鏈接Mysql數據庫并執行查詢操作的完整代碼》,本文關鍵詞  IDEA,鏈接,Mysql,數據庫,并,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《IDEA 鏈接Mysql數據庫并執行查詢操作的完整代碼》相關的同類信息!
  • 本頁收集關于IDEA 鏈接Mysql數據庫并執行查詢操作的完整代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 色女人综合网| 中国小鲜肉gary互c| 91丨九色丨国产女??原神| 日韩黄色片视频| 日产无码久久久久久精品红桃| 美女视频黄视大全视频免费的| 男舔女视频| 多P混交群体交乱文| 狂揉大乳子免费视频| 亚洲 欧美 国产 综合护士| 老司机免费福利视频无毒午夜| 91视频ios| 五月狠狠精品人妻久久干| 国产精品偷伦视频| 女贱奴请主人踩贱乳调教h| 污污在线观看| 双性诱受(H)1V1| 网红刘婷演绎停车场在线观看| 假惺惺小说全文在线阅读| 床xi替身(np)| 巜豪妇荡乳4做爰| 多人(H)男男| 午夜成人亚洲理论片在线观看简爱 | 原神莫娜裸体被?黄漫| 多男一女两根一起进去互添H漫画| 五月天激情播播| 国产寡妇婬乱一级A片在线直播APP| 国产免费高清视频在线观看不卡| 女人扒开腿让男生桶爽动漫| 免费观看黄a一级视频日本| 绿帽AV视频一区| 欧美性?XX?XX?XXX动态| 小可叫我喝她的奶水| 伊人久久大香线蕉综合影| 狠狠色噜噜狠狠狠狠888奇米| 免费网站黄色| 裸体写真在线 网站| 韩漫无删减无遮挡H漫画不良房东| 病娇被离婚以后[穿书]| 国产一卡2卡3卡四卡高清| 电影大全在线观看|