Class DBConnection
- java.lang.Object
-
- com.github.collinalpert.java2db.database.DBConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class DBConnection extends java.lang.Object implements java.lang.AutoCloseable- Author:
- Collin Alpert
- See Also:
- GitHub
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDATABASEstatic DatabaseTypesDATABASE_TYPEstatic java.lang.StringHOSTstatic booleanLOG_QUERIESstatic java.lang.StringPASSWORDstatic intPORTstatic java.lang.StringUSERNAME
-
Constructor Summary
Constructors Constructor Description DBConnection()
-
Method Summary
All Methods Instance Methods Concrete Methods 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.voidupdate(java.lang.String query)This command is used for any DDL/DML queries.voidupdate(java.lang.String query, java.lang.Object... params)This command is used for any DDL/DML queries with Java parameters.
-
-
-
Field Detail
-
HOST
public static java.lang.String HOST
-
DATABASE
public static java.lang.String DATABASE
-
USERNAME
public static java.lang.String USERNAME
-
PASSWORD
public static java.lang.String PASSWORD
-
DATABASE_TYPE
public static DatabaseTypes DATABASE_TYPE
-
PORT
public static int PORT
-
LOG_QUERIES
public static boolean LOG_QUERIES
-
-
Method Detail
-
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 void update(java.lang.String query) throws java.sql.SQLExceptionThis command is used for any DDL/DML queries.- Parameters:
query- The query to be executed.- Throws:
java.sql.SQLException- if the query is malformed or cannot be executed.
-
update
public void 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.- 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
-
-