如何使用 MS Access 等目录路径连接 MySQL 数据库文件(.sql)?

2022-01-24 00:00:00 connection mysql java

对不起,可能这是我第二次问这个问题,因为没有得到任何答案.

Sorry Maybe this is the second time I am Asking this question because of not getting any answers .

这是我的代码

    try{
    File  f = new File("Database.sql");
    if(f.exists()){
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/"+f.getName(),"","");
    }else{
        f.createNewFile();
        System.out.println("file created");
        //also do the connection
    }
    }catch(Exception ex){
       System.out.println(ex.getMessage());
    }

这是错误:

通讯链接故障最后一个成功发送到服务器的数据包是 0 毫秒前.驱动程序没有收到任何来自服务器的数据包.

这个问题的目的是:我正在为许多用户创建一个应用程序,问题是他们对计算机一无所知,我必须使其尽可能简单.

The purpose of this question is: I am creating an Application that is for many users, the problem is they don't know anything about computer and I must make it as simple as possible.

那么有什么方法可以通过目录路径连接 MYSQL,比如 MS ACCESS 吗?

或者还有其他建议吗?

谢谢.

推荐答案

初步定义

连接到 MySQL 数据库文件"是不恰当的措辞.

一个实际上连接到一个MySQL数据库服务器,它允许访问到一个数据库.所以必须有一个 MySQL 服务器可以连接,下面会详细介绍.

One actually connects to a MySQL database server, which allows access to a database. And so there must be a MySQL server to connect to, more about that below.

您的 Database.sql 文件是 数据库转储文件,也就是说,一个哑(纯文本)文件.在解释 SQL 查询之后,您需要一个专门的过程来从中提取数据:数据库服务器.

Your Database.sql file is a database dump file, that is to say, a dumb (plain text) file. You need a specialized process to extract data from it, after interpreting your SQL queries: a database server.

您可能会假设可以连接到一个文件,因为您习惯于使用 MS Access 文件.我不是 Java 和 MS Access 方面的专家,但我不明白从 Java 访问 MS Access数据库"文件实际上意味着连接到一些中间件服务器,例如 这个来自 Microsoft 的 ODBC 东西.

You might be assuming that one can connect to a file because you are used to working with MS Access files. I am not an expert neither in Java nor in MS Access, but it is my undestanding that accessing a MS Access "database" file from Java actually means connecting to some middleware server, such as this ODBC thingy from Microsoft.

无法通过目录路径连接到 MySQL 数据库服务器.唯一的原生方式是:

There is no way to connect to a MySQL database server via a directory path. The only native ways are:

  • TCP
  • 本地套接字(仅限 Unix 服务器)
  • 命名管道(仅限 Windows 服务器)
  • 共享内存(仅限 Windows 服务器)

可能有一些第三方软件提供其他协议,我不知道,但它们都归结为同一个问题:必须有一个 MySQL 服务器在某个地方运行.

There could be some third-party pieces of software around that provide other protocols, which I am not aware of, but they all reduce to the same problem: there must be a MySQL sever running somewhere.

再想,实际上有一种方法可以在不运行外部 MySQL 服务器的情况下访问 MySQL 数据:嵌入式 MySQL 服务器 C 库.我自己从未尝试过,但它看起来是独立应用程序的可行选择.但是,如果您计划在多个进程或计算机之间共享相同的 MySQL 数据,我不认为这是一个理想的解决方案.

On second thought there is actually one way to access MySQL data without an external MySQL server running: the embedded MySQL server C library. I never tried it myself, but it looks like a viable option for stand-alone applications. I do not believe, however, that it is a desirable solution if you plan to share the same MySQL data across several processes or computers.

现在我了解到您正在构建一个 Java 桌面应用程序,该应用程序基于您拥有的 SQL 转储文件形式的数据,可能是从 MySQL 服务器转储的.如果您希望您的用户能够从这个 Java 应用程序访问这些数据,我可以看到一些选项:

Now I understand you are building a Java desktop application based on data that you have in the form of a SQL dump file, probably dumped from a MySQL server. If you want your users to be able to access this data from this Java application, I can see a few options:

  • 在他们的计算机上安装 MySQL 服务器并将此转储加载到其中.很明显,但不切实际,如果我听得清楚的话.虽然我猜这个安装肯定可以由您的 Java 应用程序自动执行.

  • Install a MySQL server on their computers and load this dump into it. Obvious as hell, but impractical, if I hear you well. Although I guess this installation could certainly be performed automatically by your Java application.

在您自己的机器上安装 MySQL 服务器,并使其可以从您用户的计算机上访问.主要缺点:它需要连接您的用户.您可能还想为每个用户创建一个不同的数据库.

Install a MySQL server on a machine of your own, and make it accessible from your users' computers. Major drawback: it requires your users to be connected. You would also probably want to create a distinct database for each user.

使用真正的无服务器数据库引擎,例如 SQLite.这似乎是您的最佳选择.对于常规操作,它的 SQL 语法几乎与 MySQL 相同.它必须有大量的 JDBC 驱动程序.同样,我不是 Java 中最好的顾问,但 这个 似乎是一个认真的候选人.

Use an actually serverless database engine such as SQLite. This seems to be the best option for you. Its SQL syntax is virtually identical to MySQL for usual operations. There must be plenty of JDBC drivers for it. Again, I am not the best advisor in Java, but this one seems to be a serious candidate.

相关文章