Reputation: 341
I'm very new to Databases. I have to create a java program that interfaces with a MySQL database.
I created a database using MySQL Workbench 6.3 CE. I clicked on Local instance MySQL57, logged in and added the schema alongside the example sakila, sys and world schemas.
I then created a java project in Intellij that includes a reference to the Connector/J Jar (mysql-connector-java-5.1.38-bin.jar
). Then I use the following java code to connect to the database:
try {
String userName = "root";
String password = "password";
String url = "jdbc:mysql://localhost/mydatabase";
connection = DriverManager.getConnection(url, userName, password);
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Connected to Database");
} catch (Exception e){
e.printStackTrace();
}
This works and I am able to use connection.executeUpdate()
and connection.executeQuery()
functions as necessary to perform SQL commands.
I want to be able to export my database schema and state so that somebody can import our database and then use our java application to connect to it as is.
I've added the database to the project locally in Intellij using View|Tool Windows|Database
and then added a Data Source| MySQL
and completing the wizard to connect to it. Do I need to start a server with Intellij or what do I need to do so I can give this program to somebody so that they can run it and use the accompanying database.
Essentially how do I make it so that my program has a database to store information that will work regardless of what IDE or Database server is running on some other persons machine?
Upvotes: 0
Views: 5377
Reputation: 578
You do not have to do anything to the program to 'make the database work' - you simply connect to the database from your application as per your code snippet. The database is a self-contained unit. Once you use the executeUpdate/executeQuery
commands, you will simply change the information in the database.
The IDE really has nothing to do with the database - it simply provides a convenient interface to the database to view database information and allow editing of information / execution of queries - much in the same way the workbench does.
If you want to share your database with other people, you can simply use the Export database function (under the Server menu item) in the workbench or the mysqldump
command line utility and export to a self-contained file. By default it exports the database and the optional information in said database to sql commands. This can then be imported on the other person's database as long as that person has the mysql server and client installed on his environment.
Upvotes: 1