0

I've made a desktop based shop-management software using java that will save every product detail and sell history of a particular shop in a database over internet (phpmyadmin). I've a made a connector class and give the path of the database name which connects to the database. The problem is, for every distinct shop I need to create a different database. If i make a .exe file or installation file of my software and distribute it to users, how am i going to create different databases for each user (in this case each shop) through installation process. Is there any way of doing this dynamically or I have to create the database every time before distributing it to a new user (for a new shop actually) ?

Thanks in advance, I never make a software to distribute commercially. This is new to me.

Note 1: let me explain more about my problem, first take a look of my connector class

public class ispConnect {
static Connection connection;

public static Connection connecterDb(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://ServeName/DatabaseName","root","");
        System.out.println("Connection Established!");
        return connection;
    }catch(Exception e){
            System.out.print("Connection Failed!");
            return null;
    }
}

this the connector class which connects with the database "DatabaseName". Now if I make a installer file I can't change the "DatabaseName". Let assume I have two customer X, Y the both need my application. for both of them i need to create two database name- let DatabaseName1 for X and DatabaseName2 for Y other-wise both of their data will save into the same database - "DatabaseName" . Now my question is do i have to change database name manually for each customer every time and then make a installer again before delivering my application to them or there is other option to do it !

Erfan Ahmed
  • 1,536
  • 4
  • 19
  • 34
  • If you can connect to your database from remote location you could very well also create a new instance of database. – Blip May 06 '15 at 08:53
  • You could also look at bundling sqlite with your project, instead of mysql. A lot simpler to deploy that way. Also probably a fair chunk of work – pala_ May 06 '15 at 08:54
  • Please clarify, does your application work only with your server, or people who have bought license for your application, should deploy **different separate** MySQL server and purchased application will work on independent MySQL server that belongs to the customer ? –  May 06 '15 at 09:14
  • My application will work only with my server. Suppose person X bought my application for his shop then there must be a database on my server to store X's data. again person Y bought my application. then Y also need a separate database on my server to store his data separately. The problem is when i made my application I gave a default database name to store data. Now, how can I be able to give different database name for different user if I make a installation file of it giving a default database name ? – Erfan Ahmed May 07 '15 at 06:52

2 Answers2

3

Yes, it is possible to create a database dynamically. SQL language allows that, and you can invoke SQL expressions using java.

In SQL to create a database MyDatabase you need to invoke:

CREATE DATABASE MyDatabase

In java you can do it with this code:

Connection conn = DriverManager.getConnection("jdbc:mysql://databasehost/?user=root&password=rootpassword"); 
Statement statement = conn.createStatement();
int result=statement.executeUpdate("CREATE DATABASE MyDatabase");

Note that SQL is powerful language, you can also add/remove users, grant/revoke access permissions and so on. And you can invoke SQL expressions using Java code.

Check this link for MySQL SQL reference: http://dev.mysql.com/doc/refman/5.6/en/sql-syntax.html


Update №1:

Please note that databasehost, root username and password should not be hardcoded. Your application should ask this information upon first start, to generate the database.


Update №2:

In case you are providing MySQL server infrastructure to your customers for your application, and there's no requirement for customers to deploy and use separate independent MySQL instance on their computers, then you need a bit different approach.

Upon selling a license for your application, generate (using PHP for instance) MySQL database automatically using information provided by your customer. After that provide your customer with username/password and database name to access your server.

When your application starts, it asks for these database name, username/password, customer specifies this information at startup and starts working with your product.

As an option to provide more convenience, as you have generated server db, username and password, save this information in a (encrypted) settings file, and provide it along with the application, the customer is going to download.


Update №3

Don't hardcode database name, login and password into your java code. Declare variables:

String dbName;
String username;
String password;

And use these variables to connect to your db, like that:

connection = DriverManager.getConnection("jdbc:mysql://ServeName/" + dbName, username, password);

Store dbName, username and password in a separate text file, and let your java code to read this text file, to populate dbName, username and password variables.

You can generate this text file (also known as Java Properties file) when a user is going to download purchased software. You, on the server side, using PHP, should generate database, username and password and write this information (dbName, username and password) to the text file.

You can create and populate a text file using PHP. Here is the tutorial: http://www.w3schools.com/php/php_file_create.asp

And when text file is created, you can zip it along with your exe-file, also using PHP, here is the short howto: Creating .zip file

Hope this helps :)

Community
  • 1
  • 1
  • 1
    Note that it's probably a terrible idea to allow an application you deploy to environments you don't control to have the sort of access to your database server required in order to create a database. – pala_ May 06 '15 at 09:00
  • @pala_ The mechanism described needs root login and password, they can be parametrized and asked upon deployment to generate db on the server. There's no security flaws in this approach, from my point of view. If you think different, please elaborate your point of view. Thank you. –  May 06 '15 at 09:02
  • That's fine if you're on premises doing the deployment (that would be an environment you control - albeit temporarily). If you're distributing the package over the internet, not so much. – pala_ May 06 '15 at 09:06
  • `databasehost` is not something hardcoded, it was just an example to display a possibility to create a db. End-user should specify databasehost, root login and root password, to generate the database for the application. It may be localhost, if it is necessary for end-user. –  May 06 '15 at 09:08
  • Yes, but the implication i got from the question was he wanted the application to connect back to /his/ database server, not a client controlled one – pala_ May 06 '15 at 09:10
  • @pala_ I did not notice this requirement, that he sells the application that works with his server. I will ask him now. –  May 06 '15 at 09:11
  • 1
    @pala_ I have posted additional update to my answer. –  May 06 '15 at 09:21
  • 1
    works for me. you could probably even go so far as to inject that username and password into the archive as well, before the user downloads it – pala_ May 06 '15 at 09:22
  • @RafaelOsipov your answer is close to my solution but still i don't get it. I added some additional explanation to my question . please note that and help me out ! – Erfan Ahmed May 07 '15 at 07:20
  • @ErfanAhmedEmon moment, please. I will check and reply. –  May 07 '15 at 07:42
  • @ErfanAhmedEmon I have updated my answer, check it (for Update №3) –  May 07 '15 at 07:55
  • 1
    @RafaelOsipov Thanks !! Now I got the whole scenario. – Erfan Ahmed May 07 '15 at 11:56
0

You could bundle in the scripts which create the required database structure, thus, in short, when your application will attempt to connect to the database, and the database is not found, it would run a predefined script which would create all the necessary structures.

You could expand the above functionality to create the database whenever the application is unable to find the database, however, if you where to opt for this option I'd recommend showing a popup to the user first, informing them of what has happened and what will you do.

npinti
  • 51,780
  • 5
  • 72
  • 96