public class MySQLConnectionFactory extends Object
A Class to connect at MySQL
For this, Is used the MySQL Connector java 8.0.22
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_DRIVER
Default Driver Location
This driver refers to the latest JDBC (mysql-connector-java 8.0.22)
if you use an older JDBC such as mysql-connector-java 5.1.48, then use
OLD_DEFAULT_DRIVER |
static String |
DEFAULT_HOST
Default Database Host
|
static String |
DEFAULT_PASSWORD
Default Database Password Registred
|
static String |
DEFAULT_PORT
Default Database Port
|
static String |
DEFAULT_USER
Default Database User Registred
|
static String |
OLD_DEFAULT_DRIVER
Default Driver Location
This driver refers to older JDBCs as mysql-connector-java 5.1.48
if you use a newer JDBC, like mysql-connector-java 8.0.22, use
DEFAULT_DRIVER |
Constructor and Description |
---|
MySQLConnectionFactory() |
Modifier and Type | Method and Description |
---|---|
static void |
cleanFactory()
Clean the class fields
- DRIVER
- HOST - PORT - DATABASE - USER - PASSWORD - URL |
static boolean |
closeConnection(Connection connection)
Method responsible for closing the connection
|
static boolean |
closeConnection(Connection connection,
PreparedStatement statement)
Method responsible for closing the connection
It Calls The
closeConnection(java.sql.Connection) |
static boolean |
closeConnection(Connection connection,
PreparedStatement statement,
ResultSet result)
Method responsible for closing the connection
It Calls The
closeConnection(java.sql.Connection, java.sql.PreparedStatement) |
static Connection |
getConnection(String database)
Method responsible for making a connection
first It Calls The
loadClass(java.lang.String) function that loads the class with the data passed by parameter
after he try to import the driver, if he catch an ClassNotFoundException he will throw a RuntimeException
if he imported the driver with success, he will try to connect to the database, if he catch an SQLException he will throw RuntimeException |
static Connection |
getConnection(String host,
String port,
String database,
String user,
String password)
Method responsible for making a connection
first It Calls The
loadClass(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) function that loads the class with the data passed by parameter
after he try to import the driver, if he catch an ClassNotFoundException he will throw a RuntimeException
if he imported the driver with success, he will try to connect to the database, if he catch an SQLException he will throw RuntimeException |
static Connection |
getConnection(String driver,
String host,
String port,
String database,
String user,
String password)
Method responsible for making a connection
first It Calls The
loadClass(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) function that loads the class with the data passed by parameter
after he try to import the driver, if he catch an ClassNotFoundException he will throw a RuntimeException
if he imported the driver with success, he will try to connect to the database, if he catch an SQLException he will throw RuntimeException |
static String |
getDATABASE()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN
throw NullPointerException |
static String |
getDRIVER()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN
throw NullPointerException
The Driver receives by default: DEFAULT_DRIVER , however, it can be modified by calling the getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) method by passing briver as a parameter |
static String |
getHOST()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN
throw NullPointerException
It receives by default: DEFAULT_HOST , however, it can be
modified by calling the getConnection()
method by passing host as a parameter
if you don't know or use a standard Host, try to use the DEFAULT_HOST |
static String |
getPASSWORD()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN
throw NullPointerException
It receives by default: DEFAULT_PASSWORD , however, it can be
modified by calling the getConnection()
method by passing password as a parameter
if you don't know or use a standard Password, try to use the DEFAULT_USER |
static String |
getPORT()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN
throw NullPointerException
It receives by default: DEFAULT_PORT , however, it can be
modified by calling the getConnection()
method by passing port as a parameter
if you don't know or use a standard Port, try to use the DEFAULT_PORT |
static String |
getURL()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN
throw NullPointerException
if you don't know or use a standard Url, try to use the getURL(java.lang.String, java.lang.String, java.lang.String) |
static String |
getURL(String host,
String port,
String database)
Generate URL to connect whit Database
|
static String |
getUSER()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN
throw NullPointerException
It receives by default: DEFAULT_USER , however, it can be
modified by calling the getConnection()
method by passing User as a parameter
if you don't know or use a standard User, try to use the DEFAULT_USER |
static void |
resetFactory()
Reset the class fields
- DRIVER
- HOST - PORT - DATABASE - USER - PASSWORD - URL This function call loadClass(java.lang.String) |
public static final String DEFAULT_USER
Default Database User Registred
USER
,
Constant Field Valuespublic static final String DEFAULT_PASSWORD
Default Database Password Registred
PASSWORD
,
Constant Field Valuespublic static final String DEFAULT_DRIVER
Default Driver Location
This driver refers to the latest JDBC (mysql-connector-java 8.0.22)
if you use an older JDBC such as mysql-connector-java 5.1.48, then use OLD_DEFAULT_DRIVER
DRIVER
,
OLD_DEFAULT_DRIVER
,
Constant Field Valuespublic static final String OLD_DEFAULT_DRIVER
Default Driver Location
This driver refers to older JDBCs as mysql-connector-java 5.1.48
if you use a newer JDBC, like mysql-connector-java 8.0.22, use DEFAULT_DRIVER
DRIVER
,
DEFAULT_DRIVER
,
Constant Field Valuespublic static final String DEFAULT_HOST
Default Database Host
HOST
,
Constant Field Valuespublic static final String DEFAULT_PORT
Default Database Port
PORT
,
Constant Field Valuespublic static Connection getConnection(String database)
Method responsible for making a connection
first It Calls The loadClass(java.lang.String)
function that loads the class with the data passed by parameter
after he try
to import the driver, if he catch
an ClassNotFoundException
he will throw
a RuntimeException
if he imported the driver with success, he will try
to connect to the database, if he catch
an SQLException
he will throw
RuntimeException
database
- Name of the Database to be Connected Connection
- if everything is successful
null
- if catch
a Exception
RuntimeException
- if the method catch
SQLException
or ClassNotFoundException
public static Connection getConnection(String host, String port, String database, String user, String password)
Method responsible for making a connection
first It Calls The loadClass(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
function that loads the class with the data passed by parameter
after he try
to import the driver, if he catch
an ClassNotFoundException
he will throw
a RuntimeException
if he imported the driver with success, he will try
to connect to the database, if he catch
an SQLException
he will throw
RuntimeException
host
- Database Hostport
- Database Portdatabase
- Name of the Database to be Connecteduser
- Database User Registredpassword
- Database Password Registred Connection
- if everything is successful
null
- if catch
a Exception
RuntimeException
- if the method catch
SQLException
or ClassNotFoundException
public static Connection getConnection(String driver, String host, String port, String database, String user, String password)
Method responsible for making a connection
first It Calls The loadClass(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
function that loads the class with the data passed by parameter
after he try
to import the driver, if he catch
an ClassNotFoundException
he will throw
a RuntimeException
if he imported the driver with success, he will try
to connect to the database, if he catch
an SQLException
he will throw
RuntimeException
driver
- Driver locationhost
- Database Hostport
- Database Portdatabase
- Name of the Database to be Connecteduser
- Database User Registredpassword
- Database Password Registred Connection
- if everything is successful
null
- if catch
a Exception
RuntimeException
- if the method catch
SQLException
or ClassNotFoundException
public static boolean closeConnection(Connection connection)
Method responsible for closing the connection
connection
- Connection to be closed false
- if the Connection has not been closed,
this is because the Connection is null
or is already closed
true
- if everything is successful
RuntimeException
- if the method catch
SQLException
public static boolean closeConnection(Connection connection, PreparedStatement statement)
Method responsible for closing the connection
It Calls The closeConnection(java.sql.Connection)
connection
- Connection to be closedstatement
- PreparedStatement to be closed false
- if the PreparedStatement has not been closed,
this is because the PreparedStatement is null
or is already closed
true
- if everything is successful
RuntimeException
- if the method catch
SQLException
public static boolean closeConnection(Connection connection, PreparedStatement statement, ResultSet result)
Method responsible for closing the connection
It Calls The closeConnection(java.sql.Connection, java.sql.PreparedStatement)
connection
- Connection to be closedstatement
- PreparedStatement to be closedresult
- ResultSet to be closed false
- if the ResultSet has not been closed,
this is because the ResultSet is null
or is already closed
true
- if everything is successful
RuntimeException
- if the method catch
SQLException
public static String getURL(String host, String port, String database)
host
- Database Hostport
- Database Portdatabase
- Name of the Database to be Connectedpublic static void cleanFactory()
Clean the class fields
- DRIVER
- HOST
- PORT
- DATABASE
- USER
- PASSWORD
- URL
public static void resetFactory()
Reset the class fields
- DRIVER
- HOST
- PORT
- DATABASE
- USER
- PASSWORD
- URL
This function call loadClass(java.lang.String)
public static String getDRIVER()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN throw
NullPointerException
The Driver receives by default: DEFAULT_DRIVER
, however, it can be
modified by calling the getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
method by passing briver as a parameter
Driver location
DEFAULT_DRIVER
,
getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public static String getHOST()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN throw
NullPointerException
It receives by default: DEFAULT_HOST
, however, it can be
modified by calling the getConnection()
method by passing host as a parameter
if you don't know or use a standard Host, try to use the DEFAULT_HOST
public static String getPORT()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN throw
NullPointerException
It receives by default: DEFAULT_PORT
, however, it can be
modified by calling the getConnection()
method by passing port as a parameter
if you don't know or use a standard Port, try to use the DEFAULT_PORT
public static String getDATABASE()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN throw
NullPointerException
Name of the Database Connected
getConnection(java.lang.String)
,
getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public static String getURL()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN throw
NullPointerException
if you don't know or use a standard Url, try to use the getURL(java.lang.String, java.lang.String, java.lang.String)
public static String getUSER()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN throw
NullPointerException
It receives by default: DEFAULT_USER
, however, it can be
modified by calling the getConnection()
method by passing User as a parameter
if you don't know or use a standard User, try to use the DEFAULT_USER
Database User Registred
null
- if the USER is null
NullPointerException
- if the USER is nullDEFAULT_USER
,
getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public static String getPASSWORD()
AVOID USING IF A CONNECTION HAS NOT BEEN CREATED OR CAN throw
NullPointerException
It receives by default: DEFAULT_PASSWORD
, however, it can be
modified by calling the getConnection()
method by passing password as a parameter
if you don't know or use a standard Password, try to use the DEFAULT_USER
Database Password Registred - if not null
null
- if the PASSWORD is null
NullPointerException
- if the PASSWORD is nullDEFAULT_PASSWORD
,
getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
getConnection(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
Copyright © 2021. All rights reserved.