0% found this document useful (0 votes)
13 views20 pages

Java MySQL Connection Guide

This document provides a comprehensive guide on connecting Java applications to MySQL databases using JDBC (Java Database Connectivity). It covers the installation of XAMPP, setting up a MySQL database, and includes example Java code for viewing, adding, updating, and deleting records in a database. Additionally, it lists resources for further learning and suggests a project for practical application.

Uploaded by

hs9343965
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

Java MySQL Connection Guide

This document provides a comprehensive guide on connecting Java applications to MySQL databases using JDBC (Java Database Connectivity). It covers the installation of XAMPP, setting up a MySQL database, and includes example Java code for viewing, adding, updating, and deleting records in a database. Additionally, it lists resources for further learning and suggests a project for practical application.

Uploaded by

hs9343965
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Computer

Programming 2 Java Connection Using


MySQL

In this lesson, you will learn the step by step procedures on how to use MySQL (My

Structured Query Language database as back end of Java. You know the concept of database

where the Java has capability to connect into different back ends using objectivity connection

driver that will help to develop more advanced programs with database so that the information

or data will be stored permanently for future references using Java netbeans connecting to

MySQL (My Structured Query Language database system.

At the end of this lesson, the student should be able to:

 Define Microsoft Access database

 Know the steps in connecting to MySQL (My Structured Query Language

 Develop a program with database connection in MySQL (My Structured Query

Language

=======================================================================
Page | 1
Connecting Java into MySQL Database
JDBC stands for Java Database Connectivity, which is a standard java API for
database-independent connectivity between the Java programing Language and a wide range
of database.
 The JDBC library includes APIs for each of the tasks mentioned below that are
commonly associated with database usage.
 Making a connection to a database.
 Creating SQL or MySQL statements.
 Executing SQL or MySQL queries in the database.
 Viewing & Modifying the resulting records.
JDBC and ODBC
 ODBC (Open Database Connectivity) is a Microsoft Standard API that provides
access to a multitude of existing.
 A JDBC driver is a software component enabling a Java application to interact
with a database.
 JDBC provides this broad database support through 4 types of connectivity (via
drivers). The JBDC-ODBC bridge belongs to the first Type of Driver
 JDBC – ODBC
 In a Type 1 driver, a JDBC bridge is used to access ODBC drivers
installed on each client machine.
 Using ODBC, requires configuring on your system a Data Source Name
(DSN) that represents the target database.
 JDBC-Native API
 In a Type 2 driver, JDBC API calls are converted into native C/C++ API
calls, which are unique to the database
 JDBC-Net Pure Java

=======================================================================
Page | 2
 In a Type 3 driver, a three-tier approach is used to access databases. The
JDBC clients use standard network sockets to communicate with a
middleware application server.
 100% Pure Java
 In a Type 4 driver, a pure Java-based driver communicates directly with
the vendor's database through socket connection.
JDBC Basic Steps
 Specify the location of the data base
 ODBC
 String url = “jdbc:odbc:publication”;
 We specify that JDBC is a protocol, and ODBC is a sub protocol,
while publication is the name of the ODBC that must exist on the
system were are running The application From
 Type 4
 String url = “jdbc:mysql://localhost/act337”;
 We specify that JDBC is a protocol, while connecting to a mysql
database server and requesting a connection to the act337
database.
 Specify the driver to use for the connection
 ODBC
 String dbDriver = “[Link]”;
 We specify that JDBC ODBC bridge drivr that is built-in to JDBC.
 Type 4
 String dbDriver = “[Link]”;
 We specify that type 4 driver for Mysql. This driver need to be
presented in the classpath of the machine, you simply need to
package this with your application.
 Create an instance of the class of the JDBC Driver
 [Link](dbDriver);
 Create a connection to the database with the Driver
 Class connection
=======================================================================
Page | 3
 con = [Link](url, username, password);
 Once a driver recognize your URL, it create a connection using the properties
you specified. It then provides the Driver Manager with a [Link]
implementation representing that data base Connection. The driver manager
then passes that connection object back to the application
 Close The Connection
 [Link]();

EXAMPLE PROGRAM

Database name: DB
Table name: TableOne

Field Name Data Type

Accnum Number

Fname Text

Lname Text

Sample Data

Accnum Lname Fname


1111 Villareal Rupert
2222 Domogma Kynt Bryan
3333 Pulonan Vibanie Joseph
4444 Antonio Rommel
5555 Regulacion Gerald
6666 Alejandro Roger

Viewing Record

=======================================================================
Page | 4
Code: ViewRec
import [Link].*;
public class ViewRec
{
public static void main (String[]args)
{
String query = "SELECT * FROM tableone";
try
{
Connection con =
[Link]("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,
*.accdb)};Dbq=C:\\Users\\Bnx\\Documents\\[Link]");
Statement stmt = [Link]();
ResultSet rs = [Link](query);
while([Link]())
{
[Link]("\nAccount No:" + [Link](1));
[Link]("\nLastname: " + [Link](2));
[Link]("\nFirstname: " + [Link](3) + "\n");
}
}
catch(SQLException ex)
{
while(ex!=null)
{

=======================================================================
Page | 5
[Link]("QL Exception: " + [Link]());
ex = [Link]();
}
}
catch([Link] ex)
{
[Link]();
}
}
}

Adding Record

Output:

Code: AddRec
import [Link].*;

public class AddRec


{
public static void main (String [] args)
{
Connection con = null;

try
{
con = [Link]("jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\Bnx\\Documents\\[Link]");
Statement sta = [Link]();

int count = 0;
int c;

=======================================================================
Page | 6
c = [Link]("INSERT INTO tableone" + "(Accnum, Fname,
Lname)" + "VALUES(7777, 'iForgot', 'Adan')");
count = count + c;

[Link]("Number of rows inserted: " + count);

[Link]();
[Link]();
}
catch(Exception ex)
{
[Link]("Exception: " + [Link]());
}
}
}

Updating Record

Code: UpdRec
import [Link].*;

public class UpRec


{
public static void main (String [] args)
{
Connection con = null;

=======================================================================
Page | 7
try
{
con = [Link]("jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\Bnx\\Documents\\[Link]");
Statement sta = [Link]();

int c = [Link]("UPDATE tableone SET Fname='Adonis' WHERE


Accnum=7777");

[Link]("Record Updated.....");

[Link]();
[Link]();
}
catch(Exception ex)
{
[Link]("Exception: " + [Link]());
}
}
}

Deleting Record

Code: DelRec
import [Link].*;

public class DelRec


{
public static void main (String [] args)
{
Connection con = null;

=======================================================================
Page | 8
try
{
con = [Link]("jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\Bnx\\Documents\\[Link]");
Statement sta = [Link]();

int c = [Link]("DELETE FROM tableone WHERE


Accnum>1111");

[Link]("Records Deleted.....");
[Link]();
[Link]();
}
catch(Exception ex)
{
[Link]("Exception: " + [Link]());
}
}
}

HOW TO CONNECT JAVA INTO MySQL

There are four (4) things needed in order to connect Java in MySQL database
(1) XAMPP, (2) Net Beans (3) codes and (4) JAR file.

WHAT IS XAMPP?

XAMPP is an open source software and a package in WAMP (Windows Apache


MYSQL and PHP) or LAMP(Linux Apache MySQL and PHP).

It is open source which can be available to download from the official PHP
resource: [Link]

It runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.). It is


compatible with almost all servers used today (Apache, IIS, etc.). It supports a wide
range of database.

STEPS IN INSTALLING THE XAMPP

=======================================================================
Page | 9
1. Open the goggle search engine Type in XAMPP then press enter.
2. Choose the first option in the list of upon clicking the enter button.
3. Choose XAMPP for windows if your operating system is windows whether is
windows 8 or 7.
4. Click Download button to download, wait several minutes to complete the
downloading process.
5. After downloading the files, open it to install…

6. Just click next button until you reach the dialog box with Finished Button.

7. Check the XAMMP folder in drive C:


8. Open Xampp folder and look for HTDOCS folder.
9. Create your own folder to save all files that you will be created.

STEPS TO CHECK IF XAMPP IS WORKING PROPERLY

=======================================================================
Page | 10
1. Open the XAMPP Control Panel and click start button in Apache and MySQL module
to start working. Note (Make sure color of Apache and MySQL is both green).

2. Open the browser such as (Google Chrome, Mozilla or Internet Explorer).


3. Type in the word LOCALHOST/dashboard in the address bar, then press enter,
this will appear…

4. Type in the word LOCALHOST/xampp in the address bar, then press enter, this
will appear…

=======================================================================
Page | 11
Steps in Creating and Connecting MySQL Database using Netbeans 8.0
Environment

Step 1: Open Xampp, Create Database, name it as TEST

Step 2: Name your table to PERSONS

Step 3: Input fields names to id, name and age.

Step 4: Check the box Auto Increment on id then type int, then name
and age make it to varchar then set length into 30.

Step 5: Insert Values for the table.

Output:

=======================================================================
Page | 12
Step 6: Open Netbeans, Start a new project name it to Mysql then

change the create main class into “Main”

Step 7: Then input the codes for [Link] Listed down below.

[Link]

public class Main {


public static void main(String[] args) {
DBConnect connect = new DBConnect();
[Link]();

}
[Link]

=======================================================================
Page | 13
import [Link].*;
public class DBConnect {

private Connection con;


private Statement st;
private ResultSet rs;

public DBConnect() {
try {
[Link]("[Link]");
con =
[Link]("jdbc:mysql://localhost:3306/test","root","");
st = [Link]();

}catch(Exception ex) {

[Link]("Error: "+ex);
}
}
public void getData() {
try{

String query = "Select * from persons";


rs = [Link](query);
[Link]("Records From Database");
while([Link]()) {
String name = [Link]("name");
String age = [Link]("age");
[Link]("Name: "+name+" "+"Age: "+age+"");
}

}catch(Exception ex) {
[Link](ex);
}
}
}

Step 8: Look on the Projects Corner then Right click the <default package> Then
Create new Java Class, as shown below.

=======================================================================
Page | 14
Step 9: Name it to DBConnect.

Step 10: Then Download the ODBC Connector to mysql and java. Jar File

=======================================================================
Page | 15
Step 11: Unpack the file you downloaded then, go back to netbeans then to use
the jar file you downloaded, Look on the projects section again then right click on
the Libraries then add JAR/Folder.

Step 12: Find the folder you’ve downloaded then Open the mysql-connector-java.5.1.38-
[Link]

=======================================================================
Page | 16
Step 13: Then use the Codes for [Link] as listed down below. Then Right click on
[Link] then run file.

=======================================================================
Page | 17
Step 14: Then the output should be like this. You could see the values you’ve input on
mysql database.

OUTPUT:

=======================================================================
Page | 18
Instructions: Open the links below to read more concepts Java database connection

1. [Link]
database

2. [Link]

3. [Link]

4. [Link]

5. [Link]

Develop a simple application program like Payroll System, Information


System or Enrollment that will use Java Netbeans as front end and MySQl as back
end data base system. Choose One(1) only.
Record the video while you are coding it and submit the link which saves in
your Google drive. Submission will be be next week before the Final Exam.

=======================================================================
Page | 19
Books

1. Abante, Marmelo V. et al, Java Programming, 1st Edition, Unlimited Books


Publishing Company, 2018.

2. Pomperada, Jake R, Introduction to JAVA Programming., M


Mindshapers. 2018

3. JAVA Programming for Human Beings: The Ultimate Beginner’s


Introduction. Deshpande, Mohit. 2017 Zenva Pty Ltd.
([Link]
4. Jim Keogh, Java De Mystified “A Self Teaching Guide”, Mc Graw Hill,
Osborne, 7th Edition, 2015.

5. Rogers Cadenhead, Programming with Java in 24 Hours, 4th Edition, Sams


Publishing, 2016.

6. Walter Savitch, JAVA, An Introduction to Computer Science & Programming,


Pearson Education Asia Inc., 3rd Edition, 2014.

Websites

1. [Link]
database

2. [Link]

3. [Link]

4. [Link]

5. [Link]

=======================================================================
Page | 20

You might also like