Skip to content

A Java demo project how to connect Java GUI project to MySQL via JDBC connection

Notifications You must be signed in to change notification settings

akmalrusli363/ebankingku-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ebankingku-java

A Java demo project to demonstrate how to connect Java GUI project to MySQL via JDBC connection.

This project was built for BNCC Praetorian Training as a demonstration project how to connect Java Swing GUI to MySQL with the MySQL JDBC Connector.

Requirement

  • JDK 8 or newer - Required for executing and developing Java project
  • MySQL - For database server (manually or via XAMPP)
  • MySQL JDBC Connector - Connecting MySQL to Java Project

To use this project, you need MySQL and a latest MySQL JDBC Connector which you can download the Java Connector at (https://dev.mysql.com/downloads/connector/j/).

For MySQL Database, you can use XAMPP or install MySQL manually via (https://dev.mysql.com/downloads/)

Instruction

  1. Extract the .zip you've downloaded or clone this repository to your directory
  2. Execute the query ebankingku.sql to your MySQL Database
  3. Use or compile this project using Eclipse IDE or your favourite Java IDE (like Intellij IDEA or NetBeans)

IMPORTANT: You need to include the MySQL JDBC Connector to your project before executing it!

Basic way to connect to Database Server

Three essential object

In a Java database connector, you need to import java.sql package for every Java SQL classes and methods.

import java.sql.*; // required for every Java SQL classes and methods

After import java.sql package, create 3 Object in class definition (each from java.sql.Connection, java.sql.ResultSet, and java.sql.Statement)

private Connection connectionPath; // path for MySQL connection
private ResultSet results; // obtained from executeQuery();
private Statement statement; // the data obtained from Database server

Check availability & Define Java MySQL Connection

In a constructor, perform check for a class named com.mysql.cj.jdbc.Driver (varied between MySQL JDBC version)

// Don't forget for try-catch if the class was not found
Class.forName("com.mysql.cj.jdbc.Driver");

After check for a class, create connection by obtain connection to your MySQL Database server by:

// Define connection url 'mysql://yourMySQLpath/targetDatabase' with user 'root' and no password
String connection_url = "jdbc:mysql://yourMySQLpath/targetDatabase";
String username = "root";
String password = "";

// Perform connection to a MySQL Database server using connection_url, username, password
connect = DriverManager.getConnection(connection_url, username, password);
st = connect.createStatement(resultSetType, resultSetConcurrency);

Don't forget for try-catch clause for class checking and SQL Connection attempts

Notice:

  • resultSetType: TYPE_FORWARD_ONLY(1003), TYPE_SCROLL_INSENSITIVE(1004), TYPE_SCROLL_SENSITIVE(1005)
  • resultSetConcurrency: CONCUR_READ_ONLY(1007), CONCUR_UPDATABLE(1008)

Java SQL (java.sql.ResultSet) documentation - ResultSet JavaDocs

And finally, don't forget to close MySQL Database connection by connection.close() at a closeDB() method:

public void close() throws SQLException {
  connect.close();
}

Execute and update database query

To perform selection query, you need obtain result by executeQuery(String query) to ResultSet as results from the query. For executing the query that modify, input, or delete data from table, use executeUpdate(String query) to perform a query to update their table or database state.

If you need to perform execute or update database query, use two following methods as follow:

// Read and obtain data from database
public ResultSet execute(String query) throws SQLException {
  rs = st.executeQuery(query);
  return rs;
}

// Perform change, input, or delete data query for database
public void update(String query) throws SQLException {
  st.executeUpdate(query);
}

Further documentation

About

A Java demo project how to connect Java GUI project to MySQL via JDBC connection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages