To connect Java application with the MySQL database, we need to follow 5 following steps.
Java is a general-purpose, open-source and free programming language. It originally developed by Sun Microsystems in 1995 and more than 3 billion devices run. Download the latest version of MySQL Connector/ODBC for Mac - Connect MySQL server to ODBC database API. Read 2 user reviews of MySQL Connector/ODBC on MacUpdate.
In this example we are using MySql as the database. So we need to know following informations for the mysql database:
- Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
- Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and sonoo is the database name. We may use any database, in such case, we need to replace the sonoo with our database name.
- Username: The default username for the mysql database is root.
- Password: It is the password given by the user at the time of installing the mysql database. In this example, we are going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need to create database first.
Example to Connect Java Application with mysql database
In this example, sonoo is the database name, root is the username and password both.
The above example will fetch all the records of emp table.
To connect java application with the mysql database, mysqlconnector.jar file is required to be loaded.
Two ways to load the jar file:
- Paste the mysqlconnector.jar file in jre/lib/ext folder
- Set classpath
1) Paste the mysqlconnector.jar file in JRE/lib/ext folder:
Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar file here. |
2) Set classpath:
There are two ways to set the classpath:
|
How to set the temporary classpath
open command prompt and write: |
Mysql J Connector Download
How to set the permanent classpath
Mysql Download Connector Mac Javatpoint
Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path to the mysqlconnector.jar file by appending mysqlconnector.jar;.; as C:foldermysql-connector-java-5.0.8-bin.jar;.;
How To Embed Mysql Database Into Java Application
Mysql Connector Java 8 Jar
31-08-2014![Java Java](/uploads/1/1/7/8/117802810/111658808.jpg)
Mysql Connector Java Download Mac
Embedding mysql into java application is not straightforward. Therefore, I will try to explain necessary operations step by step.
1. Download this library file then extract jar files into a folder.
2. Create a Java application and add downloaded jar files into the application lib folder
If you use maven application add following dependencies into pom.xml file
Note: Above dependencies can be found in maven central repository, but I strongly suggest that use these downloaded jar files by installing them locally by using following mvn commands:
3. After step 1 and step create a class named as ConnectorMXJObjectTestExample.java and add following codes:
If you see the following error, then you have to override platform-map.properties file located in mysql-connector-mxj-gpl-db-files-5.0.12.jar
New platform-map.properties file:
Note: Only
Windows_8-x86=Win-x86
line is added. To override this file, you have to create a new file in the classpath with same name(platform-map.properties ) and copy above content into this file. If you use maven, copy this new file into resources folder.
Result
When you run ConnectorMXJObjectTestExample.java file, automatically a folder named as mysql-mxj is created in java.io.tmpdir location. In Windows, this location is
C:Users{UserName}AppDataLocalTemp
All necessary files for an embedded mysql database is located in the mysql-mxj folder. You can change this folder name if you want.
Database Name: our_test_app
Database Port: 3336
User Name: olyanren
Password: 1987
Some Important Notes
1. I created mysql-connector-mxj-gpl-db-files-5.0.12.jar file from this file by removing platforms except Windows platform.
2. After version 5.0.12, development of MySQLConnector/MXJ has been discontinued.
3. For more information, please click
To download our sample application, please click