Class DBConnection
java.lang.Object
com.github.collinalpert.java2db.database.DBConnection
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class DBConnection
extends java.lang.Object
implements java.io.Closeable
- Author:
- Collin Alpert
- See Also:
- GitHub
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDATABASESpecifies the name of the database to connect to.static java.lang.StringHOSTSpecifies the hostname/ip address of the database.static booleanLOG_QUERIESConstant which determines if the queries generated by Java2DB will be logged in the console.static java.lang.StringPASSWORDSpecifies the password to log in on the database with.static intPORTSpecifies the port to connect to the database on.static java.lang.StringUSERNAMESpecifies the username to log in on the database with. -
Constructor Summary
Constructors Constructor Description DBConnection() -
Method Summary
Modifier and Type Method Description voidclose()Closes the connection to the database.java.sql.ResultSetexecute(java.lang.String query)Executes a DQL statement on the database without Java parameters.java.sql.ResultSetexecute(java.lang.String query, java.lang.Object... params)Executes a DQL statement on the database with Java parameters.booleanisOpen()Determines if a connection to the database still exists or not.booleanisValid()Checks if the connection is valid/successful.longupdate(java.lang.String query)This command is used for any DDL/DML queries.longupdate(java.lang.String query, java.lang.Object... params)This command is used for any DDL/DML queries with Java parameters.
-
Field Details
-
HOST
public static java.lang.String HOSTSpecifies the hostname/ip address of the database. -
DATABASE
public static java.lang.String DATABASESpecifies the name of the database to connect to. -
USERNAME
public static java.lang.String USERNAMESpecifies the username to log in on the database with. -
PASSWORD
public static java.lang.String PASSWORDSpecifies the password to log in on the database with. -
PORT
public static int PORTSpecifies the port to connect to the database on. This property is optional. If not specified, it will be set to 3306, the default port of MySQL. -
LOG_QUERIES
public static boolean LOG_QUERIESConstant which determines if the queries generated by Java2DB will be logged in the console.
-
-
Constructor Details
-
DBConnection
public DBConnection()
-
-
Method Details
-
isValid
public boolean isValid()Checks if the connection is valid/successful.- Returns:
- True if connection was successful, false if not.
-
execute
public java.sql.ResultSet execute(java.lang.String query) throws java.sql.SQLExceptionExecutes a DQL statement on the database without Java parameters.- Parameters:
query- The query to be executed.- Returns:
- The
ResultSetcontaining the result from the DQL statement. - Throws:
java.sql.SQLException- if the query is malformed or cannot be executed.
-
execute
public java.sql.ResultSet execute(java.lang.String query, java.lang.Object... params) throws java.sql.SQLExceptionExecutes a DQL statement on the database with Java parameters.- Parameters:
query- The query to be executed.params- The Java parameters to be inserted into the query.- Returns:
- The
ResultSetcontaining the result from the DQL statement. - Throws:
java.sql.SQLException- if the query is malformed or cannot be executed.
-
update
public long update(java.lang.String query) throws java.sql.SQLExceptionThis command is used for any DDL/DML queries.- Parameters:
query- The query to be executed.- Returns:
- the last generated ID. This return value should only be used with INSERT statements.
- Throws:
java.sql.SQLException- if the query is malformed or cannot be executed.
-
update
public long update(java.lang.String query, java.lang.Object... params) throws java.sql.SQLExceptionThis command is used for any DDL/DML queries with Java parameters.- Parameters:
query- The query to be executed.params- The Java parameters to be inserted into the query.- Returns:
- the last generated ID. This return value should only be used with INSERT statements.
- Throws:
java.sql.SQLException- if the query is malformed or cannot be executed.
-
isOpen
public boolean isOpen()Determines if a connection to the database still exists or not.- Returns:
Trueif a connection exists,falseif not. This method will returnfalseif an exception occurs.
-
close
public void close()Closes the connection to the database.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-