如何使用目录路径与MS Access连接MySQL数据库文件(.sql)? [英] How to Connect with MySQL Database File( .sql ) using Directory Path Like MS Access?

查看:283
本文介绍了如何使用目录路径与MS Access连接MySQL数据库文件(.sql)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起也许这是我第二次提出这个问题,因为没有得到任何答案。



这是我的代码

  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);
//也做连接
}
} catch(Exception ex){
System.out.println(ex.getMessage());
}

这里是错误:



通信链路故障
成功发送到服务器的最后一个数据包为0毫秒前。



此问题的目的是:
我正在创建一个应用程序这是为许多用户,问题是他们不知道任何关于计算机,我必须使它尽可能简单。



所以有任何方式通过目录路径与MS ACCESS连接?



还是还有其他建议吗?



感谢。

解决方案

初步定义



一个实际上连接到MySQL数据库 / em>,允许访问数据库。因此,必须有一个MySQL服务器来连接,下面有更多内容。



您的 Database.sql 文件是数据库转储文件,也就是说,愚蠢(纯文本)文件。



您可能会假设您可以连接到一个文件,您可能需要一个专门的过程来解析数据。 ,因为您习惯使用MS Access文件。我不是Java和MS Access的专家,但是我从来不知道从Java访问MS Access数据库文件实际上意味着连接到一些中间件服务器,如来自Microsoft的此ODBC事情



答案



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




  • TCP

  • 本地套接字(仅限Unix服务器)

  • 命名管道Windows服务器)

  • 共享内存(仅限Windows服务器)



- 提供其他协议的软件,我不知道,但他们都减少到相同的问题:必须有一个MySQL服务器运行在某个地方。



第二个想法是,实际上有一种方法访问MySQL数据,而没有外部MySQL服务器运行:嵌入式MySQL服务器C库。我从来没有尝试过它,但它看起来像一个可行的选择独立的应用程序。但是,如果您计划在多个进程或计算机上共享相同的MySQL数据,那么我不相信这是一个可取的解决方案。



解决方法



现在我明白你正在构建一个基于数据的Java桌面应用程序,这些数据是以SQL转储文件的形式存在的,可能是从MySQL服务器转储的。如果您希望用户能够从此Java应用程序访问此数据,我可以看到以下几个选项:




  • 安装在他们的计算机上的MySQL服务器,并加载此转储到它。显然是地狱,但不切实际,如果我听到你很好。虽然我猜这个安装肯定可以由您的Java应用程序自动执行。


  • 在您自己的机器上安装MySQL服务器,用户的计算机。主要缺点:它需要您的用户连接。


  • 使用实际无服务器的数据库引擎,例如 SQLite 。这似乎是你最好的选择。它的SQL语法与MySQL通常的操作基本相同。必须有大量的JDBC驱动程序。再次,我不是Java中最好的顾问,但这一个似乎是一个严重的候选人。



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

this is my Code

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

and Here is the error :

Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

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.

So is there any way to connect with MYSQL like MS ACCESS via Directory Path ?

OR is there any other suggestion instead ?

Thanks .

解决方案

Preliminary definitions

"Connect to a MySQL database file" is improper phrasing.

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.

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.

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.

The answer

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

  • TCP
  • Local socket (Unix servers only)
  • Named pipes (Windows servers only)
  • Shared memory (Windows servers only)

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.

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.

The workarounds

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:

  • 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.

  • 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.

  • 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.

这篇关于如何使用目录路径与MS Access连接MySQL数据库文件(.sql)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆