0

I have created a java application in netbeans, and I intend to create an installer for the project.

For this I have created a jar file of the application, but I'm using the mysql database localhost.

How can I generate Jar File with Mysql localhost .

Can anyone help me please?

Thanks and regards

-----------------------------Edit---------------------------------------

Maybe not the best way to express myself, what I mean is that the database is created locally (localhost).

The connection of the application with the database is done this way:

Class.forName("com.mysql.jdbc.Driver");

return driverManager.getConnection("jdbc:mysql://localhost/database","root", "root");

I want to create a jar file of my application that has a database created locally.

4
  • localhost is no way related to jar and mysql. Commented Mar 12, 2014 at 11:38
  • maybe not the best way to express myself, what I mean is that the database is created locally (localhost). The connection of the application with the database is done this way: Class.forName("com.mysql.jdbc.Driver"); return driverManager.getConnection("jdbc:mysql://localhost/database","root", "root"); I want to create a jar file of my application that has a database created locally. Commented Mar 12, 2014 at 11:45
  • jar and mysql are two different entities. You should bundle both jar and the process steps of checking and installing mysql for a target machine. Commented Mar 12, 2014 at 11:47
  • jar file cannot be generated with Mysql localhost Commented Mar 12, 2014 at 11:52

1 Answer 1

1

I am going to explain a few things:

  1. You do not need to hard - code the Connect URL into your code. This is why you are asking for ways of creating the database as localhost. I suggest you do not hard code the Connect URL in the code. Instead write it in an editable File either a Properties file or even a text file. Let the Application read the Editable file and pass the Parameters to the Code.

  2. An Application running in your Local Machine where the database is will connect using Localhost. But a the same Application running remotely from another Machine whether in the Internet or Local access network will not Connect this way.That is why I am insisting on NOT Hard-Coding the Connect String.

  3. The database Name, user, and Password Including the Host will change from time to Time depending on which environment the Application is running in. So again if the environment changes and the variables are not the same the Application will Not Connect to the database.

Suggestion:

User a Properties file:

db.host=192.168.1.23
db.user=root
db.password=root
db.dbname=database

Load the file as a Properties file:

Properties prop = new Properties();
    InputStream input = null;

    try {

        input = new FileInputStream("config.properties");

        // load a properties file
        prop.load(input);

        // get the property value and print it out
        System.out.println(prop.getProperty("db.host"));
        System.out.println(prop.getProperty("db.user"));
        System.out.println(prop.getProperty("db.password"));
            System.out.println(prop.getProperty("db.dbname"));
         //PASS YOUR CONNECT STRING 
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn =  DriverManager.getConnection("jdbc:mysql://" + prop.getProperty("db.host") + "/" + prop.getProperty("db.dbname"), prop.getProperty("db.user"), prop.getProperty("db.password"));

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

This way you will never have to worry about what database the application is running on as you will just have to edit the config.properties and the Application will do the rest.

I hope I gave you an answer or better still other ideas on how to handle your situation.

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

2 Comments

Thank you for reply!!How can i do the Properties file, to do the rest?
Write a text file like I have shown above and save it as a .properties. the n Load it with the code as I have shown.

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.