0

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 .

Azad
  • 5,047
  • 20
  • 38

2 Answers2

5

AFAIK, you can't plug in a file name in the JDBC url for MySQL. MySQL needs to be running, and you need to connect to it via its TCP port. Something like:

jdbc:mysql://localhost:3306/yourDatabaseName

See http://dev.mysql.com/doc/refman/5.6/en/connector-j-reference-configuration-properties.html

Jeremiah Orr
  • 2,620
  • 1
  • 18
  • 24
  • Thanks Bro ,you know the thing is I don't know how to do it, I tried 5-10 ways the same error occurs So Can you Give me a real-example? – Azad Mar 05 '13 at 20:11
  • 1
    @AzadEng http://dev.mysql.com/usingmysql/java/ has links to some examples you might want to look into. – Larry Shatzer Mar 05 '13 at 20:58
  • 1
    That is a real example; if you're running your database locally, the host would be localhost, the default port is 3306, and "yourDatabaseName" would be replaced with whatever you named your database. If you don't know how to start MySQL, here are instructions for Windows: http://dev.mysql.com/doc/refman/5.0/en/windows-start-command-line.html Similar guides exist for OS X and Linux. – Jeremiah Orr Mar 06 '13 at 15:31
  • links are out of date – Amiri Jun 01 '17 at 11:11
4

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.

RandomSeed
  • 29,301
  • 6
  • 52
  • 87
  • Well , I am researching on it actually I test it on my computer but let me test it on other computers. Thank you very much +1 for now and +100 after testing :) – Azad Mar 08 '13 at 16:37
  • One other question, can I share this database between some computer like MySQL ? – Azad Mar 10 '13 at 08:44
  • AFAIK, SQlite is not really designed to be accesses from several, concurrent processes. But I suppose you could share the sqlite file by placing it on a network location. If the driver recognizes network paths (like `\\fileserver\db.sqlite`), then you are good to go. If not, then you could mount the network share as a virtual drive. If the database is meant to be modified by concurrent threads, however, there are some issues to be taken care of. I suggest you post another question about sharing a SQLite database across a network, so that experts in SQLite can jump in. – RandomSeed Mar 11 '13 at 10:11
  • Nevertheless, I believe that if you need a database to be accessed from several unsynchronized processes/computers, then you actually need a real database server. – RandomSeed Mar 11 '13 at 10:17