You are on page 1of 6

Java to MySql

Create Database Connection with Java Program

Step 1: Create a Java Project.

Step 2: Copy DBConnection and QueryProcessor classes to your Java Project.

Your project will look like this.

DBConnection.class – intended for connecting your database and java program using the needed
privileges like database username and database password

QueryProcessor – intended for calling sql query for reading and writing data or information to your
database. Queries like insert into, select, update and delete .

Step 3: Configure your database in XAMPP Control panel.

3.1 Create a database: Example. Library_system


3.2 Add Privileges to your database library_system.

Click your database name, and Click Privileges.

And Click Add user account


3.3 Add username and password to your database.

Username

Host name: Localhost

Password

Check All

Click the Go button to save

*Remember your username and password of your database, so that its easier for you to configure the
DBConnection.class in your Java Project.

3.4 Confirmation that you have created a new Account to your Database.

Step 4. Using the username and password in your database, Open the DBConnection.class in your Java
Project and modify the jdbc:driver.
Database password
Database name

Database username

Step 5. Add MySQL JDBC Driver libary to your Java Project.

*MySQL JDBC Driver is a library or API that connects MySQL and Java Projects. Without this library in
your project, it will cause an error.

5.1 Go to Libray in your Java Project.

5.2 Right click on the Library Folder of your Java Project, and Select “Add Library”.

Look for MYSQL JDBC DRIVER,

. And click the “Add Library” to


add this in your Java Project

Step 6. Calling the QueryProcessor.class and DBConnection.class to determine that you are
successfully connected with MySql.
To do that. Let us remember , that QueryProcessor and DBConnection.class are separate classes,
separate to your main class. In order to access them is to create an object for them. But Before that,
let us analyze first diagram.

SimpleDatabaseProgram.java
1
(Main Class)

4
2
QueryProcessor.java DBConnection.java

*This diagram shows that in order to determine that you are connected to your MySQL. The main
class or the class, will call the QueryProcessor.class and the QueryProcessor.class will call the
DBConnection.class and the DBConnection will return the result Successfully Connected or Not.

If the connection is not successful, you cannot perform the query like. Select, insert into, update,
delete and etc.

6.1 In your main class, to call the QueryProcessor.class, we must create an Object for that class.

Instantiation that the


db will the Object for
our the
QueryProcessor.class

Step 7. Run the Program.

You might also like