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

}

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、写个测试连接的类,测试一下 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执行查询操作 的资料请关注www.887551.com其它相关文章!