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 .

9
  • Check this link: stackoverflow.com/questions/6865538/… Commented Mar 5, 2013 at 19:51
  • Before your comment I was reading this linke hhhh Thanks anyway :) Commented Mar 5, 2013 at 19:53
  • 1
    Your .sql file is likely not a database file, but a script. Commented Mar 5, 2013 at 19:55
  • 1
    If you did not get an answer (or a good one), you should read stackoverflow.com/faq#bounty Commented Mar 5, 2013 at 20:00
  • 2
    Correct. You may also want to delete your other question and update this question with any differing details between the two. Commented Mar 5, 2013 at 21:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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?
@AzadEng dev.mysql.com/usingmysql/java has links to some examples you might want to look into.
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: dev.mysql.com/doc/refman/5.0/en/windows-start-command-line.html Similar guides exist for OS X and Linux.
links are out of date
4
+100

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.

4 Comments

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 :)
One other question, can I share this database between some computer like MySQL ?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.