简体   繁体   English

如何使用 Electron 通过 JDBC 连接到 MySQL 数据库

[英]How to connect to a MySQL Database with JDBC using Electron

So I've been tinkering (fresh out of college) with creating a DB app via Electron as practice.所以我一直在修修补补(大学刚毕业)通过 Electron 创建一个数据库应用程序作为练习。 I've successfully created a Java-only program that uses JDBC to connect to an MySQL database, but now I have to integrate that with an Electron GUI somehow and frankly I have no idea how to proceed.我已经成功创建了一个使用 JDBC 连接到 MySQL 数据库的纯 Java 程序,但现在我必须以某种方式将它与电子 GUI 集成,坦率地说,我不知道如何继续。

Most things I've searched explain that connecting to a database via JavaScript is possible though completely not recommended due to the security issues, so I'd like to do this as securely as possible.我搜索过的大多数内容都说明可以通过 JavaScript 连接到数据库,但由于安全问题完全不推荐,因此我希望尽可能安全地执行此操作。

Would anyone know how to run Java within an Electron application to connect to a database, or is there some other method I'm overlooking that makes this seemingly Sisyphean task more simple?有谁知道如何在 Electron 应用程序中运行 Java 以连接到数据库,或者是否有其他一些我忽略的方法使这个看似 Sisyphean 的任务更加简单?

Sorry for the possible ignorance here, still learning the ins and outs of different frameworks and such.很抱歉这里可能的无知,仍在学习不同框架等的来龙去脉。 Any help is appreciated.任何帮助表示赞赏。

You should use node.js to access mysql.您应该使用 node.js 访问 mysql。 I find some example as below to be helpful to you.我发现下面的一些例子对你有帮助。 Pls see here https://ourcodeworld.com/articles/read/259/how-to-connect-to-a-mysql-database-in-electron-framework请看这里https://ourcodeworld.com/articles/read/259/how-to-connect-to-a-mysql-database-in-electron-framework

To avoid link page unavailable, I pasted code here:为了避免链接页面不可用,我在这里粘贴了代码:

var mysql = require('mysql');

// Add the credentials to access your database
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : null, // or the original password : 'apaswword'
    database : 'ourcodeworld-database'
});

// connect to mysql
connection.connect(function(err) {
    // in case of error
    if(err){
        console.log(err.code);
        console.log(err.fatal);
    }
});

// Perform a query
$query = 'SELECT * FROM `myTableName` LIMIT 10';

connection.query($query, function(err, rows, fields) {
    if(err){
        console.log("An error ocurred performing the query.");
        console.log(err);
        return;
    }

    console.log("Query succesfully executed", rows);
});

// Close the connection
connection.end(function(){
    // The connection has been closed
});

If you want your application more secure, you can follow these solution.如果您希望您的应用程序更安全,您可以遵循这些解决方案。

** solution 1: package your electron into exe file. ** 解决方案1:将您的电子打包成exe 文件。 so db connection info can not be seen by others.所以其他人无法看到数据库连接信息。 You can find more suggestion in this site.您可以在此站点中找到更多建议。

** solution 2: You can build an independent service to process mysql DB using node.js or Java(Spring). ** 方案二:可以使用node.js或者Java(Spring)搭建一个独立的服务来处理mysql DB。 Then using restful in https to access service to obtain info from DB.然后在 https 中使用 restful 访问服务以从数据库获取信息。 It is secure too它也很安全

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM