最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IDEA 鏈接Mysql數(shù)據(jù)庫(kù)并執(zhí)行查詢操作的完整代碼

 更新時(shí)間:2021年05月20日 10:26:33   作者:簡(jiǎn)簡(jiǎn)單單OnlineZuozuo  
這篇文章主要介紹了IDEA 鏈接Mysql數(shù)據(jù)庫(kù)并執(zhí)行查詢操作的完整代碼,代碼不難,詳細(xì)大家看完本文肯定有意向不到的收獲,感興趣的朋友跟隨小編一起看看吧

 1、先寫個(gè) Mysql 的鏈接設(shè)置頁(yè)面

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);
        }
    }

}

2、然后簡(jiǎn)單的寫個(gè) JDBC 操作數(shù)據(jù)庫(kù)的支持類

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 {


    /**
     * 執(zhí)行查詢語(yǔ)句的返回結(jié)果
     */
    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 {


        /**
         * 執(zhí)行sql 并返回 map 數(shù)據(jù)
         *
         * @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);

                // 基礎(chǔ)信息
                ResultSetMetaData metaData = resultSet.getMetaData();
                // 返回了多少個(gè)字段
                int columnCount = metaData.getColumnCount();


                while (resultSet.next()) {
                    Map<String, Object> valueMap = new LinkedHashMap<>();
                    for (int i = 0; i < columnCount; i++) {
                        // 這個(gè)字段是什么數(shù)據(jù)類型
                        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, "數(shù)據(jù)庫(kù)連接成功", NotificationType.INFORMATION);
                }
            } catch (Exception e) {
                NotifyUtils.notifyUser(null, "數(shù)據(jù)庫(kù)連接失敗", 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 語(yǔ)句模版
     */
    public enum Template {

        SELECT("SELECT * FROM x WHERE 1 = 1 AND ", "SELECT * FROM {} WHERE 1 = 1 AND ", "查詢語(yǔ)句"),
        UPDATE("UPDATE x SET x = x WHERE 1 = 1 AND ", "UPDATE {} SET x = x WHERE 1 = 1 AND ", "更新語(yǔ)句"),
        DELETE("DELETE FROM x WHERE 1 = 1 ", "DELETE FROM {} WHERE 1 = 1 ", "刪除語(yǔ)句"),
        INSERT("INSERT INTO * (x) VALUES (x) ", "INSERT INTO {} (x) VALUES (x) ", "新增語(yǔ)句"),
        ;

        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;
        }

        /**
         * 模版內(nèi)容:默認(rèn)值
         */
        private final String defaultValue;
        /**
         * 動(dòng)態(tài)內(nèi)容
         */
        private final String dynamicValue;
        /**
         * 內(nèi)容描述
         */
        private final String describe;


    }

}

3、寫個(gè)測(cè)試連接的類&#xff0c;測(cè)試一下 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數(shù)據(jù)庫(kù)并執(zhí)行查詢操作的完整代碼的詳細(xì)內(nèi)容,更多關(guān)于IDEA 鏈接Mysql執(zhí)行查詢操作 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • MySQL中NULL對(duì)索引的影響深入講解

    MySQL中NULL對(duì)索引的影響深入講解

    這篇文章主要給大家介紹了關(guān)于MySQL中NULL對(duì)索引的影響的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • mysql如何利用binlog進(jìn)行數(shù)據(jù)恢復(fù)詳解

    mysql如何利用binlog進(jìn)行數(shù)據(jù)恢復(fù)詳解

    MySQL的binlog日志是MySQL日志中非常重要的一種日志,下面這篇文章主要給大家介紹了關(guān)于mysql如何利用binlog進(jìn)行數(shù)據(jù)恢復(fù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-10-10
  • MySQL5.6.31 winx64.zip 安裝配置教程詳解

    MySQL5.6.31 winx64.zip 安裝配置教程詳解

    這篇文章主要介紹了MySQL5.6.31 winx64.zip 安裝配置教程詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • MySQL配置文件my.ini的使用解讀

    MySQL配置文件my.ini的使用解讀

    這篇文章主要介紹了MySQL配置文件my.ini的使用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 安裝mysql 8.0.17并配置遠(yuǎn)程訪問的方法

    安裝mysql 8.0.17并配置遠(yuǎn)程訪問的方法

    這篇文章主要介紹了安裝mysql 8.0.17并配置遠(yuǎn)程訪問的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • MySQL的事件調(diào)度器使用介紹

    MySQL的事件調(diào)度器使用介紹

    這篇文章主要介紹了MySQL的事件調(diào)度器使用介紹,本文講解了事件調(diào)度器的開啟、創(chuàng)建、修改、刪除等操作的使用實(shí)例,需要的朋友可以參考下
    2015-06-06
  • mysql自動(dòng)備份多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

    mysql自動(dòng)備份多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

    本文主要介紹了mysql自動(dòng)備份多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • SQL行列轉(zhuǎn)換超詳細(xì)四種方法詳解

    SQL行列轉(zhuǎn)換超詳細(xì)四種方法詳解

    在數(shù)據(jù)分析的面試中SQL問題基本上是必問的,其中SQL行列轉(zhuǎn)換的問題出鏡率極其高,重要性也是不言而喻,下面這篇文章主要給大家介紹了關(guān)于SQL行列轉(zhuǎn)換超詳細(xì)四種方法的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Mysql8.0.22解壓版安裝教程(小白專用)

    Mysql8.0.22解壓版安裝教程(小白專用)

    這篇文章主要介紹了Mysql8.0.22解壓版安裝教程(小白專用),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 關(guān)于SQL建表語(yǔ)句使用詳解

    關(guān)于SQL建表語(yǔ)句使用詳解

    在SQL數(shù)據(jù)庫(kù)設(shè)計(jì)中,創(chuàng)建表是基本操作,涉及定義表結(jié)構(gòu),包括列名、數(shù)據(jù)類型和約束等,本文詳細(xì)介紹建表語(yǔ)句,通過(guò)示例幫助理解,常見數(shù)據(jù)類型包括整數(shù)、浮點(diǎn)數(shù)、字符串、日期時(shí)間等,約束確保數(shù)據(jù)完整性,包括主鍵、唯一、非空、默認(rèn)值、外鍵和檢查約束
    2024-10-10

最新評(píng)論

河源市| 郸城县| 清新县| 拜泉县| 常熟市| 巴里| 南安市| 乐亭县| 沙田区| 常宁市| 岚皋县| 海阳市| 光泽县| 蒙阴县| 万安县| 镇江市| 曲麻莱县| 腾冲县| 大悟县| 咸阳市| 玉树县| 盈江县| 镇沅| 巴林右旗| 泰宁县| 海南省| 喜德县| 华蓥市| 密山市| 江安县| 厦门市| 宜兴市| 当雄县| 海阳市| 彭山县| 奎屯市| 南部县| 博乐市| 遵化市| 碌曲县| 武川县|