Connecting to MySQL using Java
Download
Report
Transcript Connecting to MySQL using Java
Connecting to MySQL using
Java
By:
– Required to use
Java.sql so that
we can use
Connection and
Queries using
strings.
– Javax.swing.*
needed for
components used
in GUI
– Java.io for input
and output
functions.
– Java.awt.* for
interactivity with
the GUI.
Connection class comes
from Java.sql.* library.
Used to establish a
connection with the
database and starts with
a null connection until
told to connect.
Connecting to the
database is enclosed
inside a try block to
catch exceptions in
connecting, so program
doesn’t crash and
problems can be fixed
later.
ActionPerformed
grabs the actions from
buttons, and depending
on which button is
clicked, uses the
appropriate action.
There are only 2 types
of statements in SQL,
update type statements
and Query type
statements.
An Update statement
is used to modify the
database in some way.
Update is a simple
process, the function
returns an integer of
the number of columns
returned, or -1 if the
update failed.
A query type statement
is used when we are
retrieving data from the
database
A ResultSet, that
executeQuery returns,
is the data retrieved in
a 2 dimensional array
similar to the format of
a database.
A ResultSet can
contain multiple data
types. In this case, it
cannot be accessed as a
normal 2 dimensional
array and we have to
use special get
functions to retrieve the
data.
The amount of columns
returned by a query statement
cane be retrieved using the
functions of a
ResultSetMetaData., this
holds information about the
data returned. For example:
the name of the column, how
many columns, and the SQL
data type of the column.
All data retrieved in this
program is used as strings to
be printed to the TextArea in
the GUI.
If we wanted to actually use
the data for specific purposes,
there are other functions to
get the true data type, rather
than strings.
Rs.next() points returns a true
if there is another row of data
in the 2 dimensional array. It
also move the ResultSet to
that row for retrieval with a
ResultSetMetaData data type.