Class 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.String DATABASE
      Specifies the name of the database to connect to.
      static DatabaseTypes DATABASE_TYPE
      Specifies the type of the database.
      static java.lang.String HOST
      Specifies the hostname/ip address of the database.
      static boolean LOG_QUERIES
      Constant which determines if the queries generated by Java2DB will be logged in the console.
      static java.lang.String PASSWORD
      Specifies the password to log in on the database with.
      static int PORT
      Specifies the port to connect to the database on.
      static java.lang.String USERNAME
      Specifies the username to log in on the database with.
    • Constructor Summary

      Constructors 
      Constructor Description
      DBConnection()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the connection to the database.
      java.sql.ResultSet execute​(java.lang.String query)
      Executes a DQL statement on the database without Java parameters.
      java.sql.ResultSet execute​(java.lang.String query, java.lang.Object... params)
      Executes a DQL statement on the database with Java parameters.
      boolean isOpen()
      Determines if a connection to the database still exists or not.
      boolean isValid()
      Checks if the connection is valid/successful.
      void update​(java.lang.String query)
      This command is used for any DDL/DML queries.
      void update​(java.lang.String query, java.lang.Object... params)
      This command is used for any DDL/DML queries with Java parameters.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • HOST

        public static java.lang.String HOST
        Specifies the hostname/ip address of the database.
      • DATABASE

        public static java.lang.String DATABASE
        Specifies the name of the database to connect to.
      • USERNAME

        public static java.lang.String USERNAME
        Specifies the username to log in on the database with.
      • PASSWORD

        public static java.lang.String PASSWORD
        Specifies the password to log in on the database with.
      • DATABASE_TYPE

        public static DatabaseTypes DATABASE_TYPE
        Specifies the type of the database. Currently MySQL and Microsoft SQL are supported. This property is optional. Default is MySQL.
      • PORT

        public static int PORT
        Specifies the port to connect to the database on. This property is optional. If not specified, it will be set by the default ports based on the DATABASE_TYPE.
      • LOG_QUERIES

        public static boolean LOG_QUERIES
        Constant which determines if the queries generated by Java2DB will be logged in the console.
    • Constructor Detail

      • DBConnection

        public DBConnection()
    • 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.SQLException
        Executes a DQL statement on the database without Java parameters.
        Parameters:
        query - The query to be executed.
        Returns:
        The ResultSet containing 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.SQLException
        Executes 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 ResultSet containing 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.SQLException
        This 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.SQLException
        This 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:
        True if a connection exists, false if not. This method will return false if an exception occurs.
      • close

        public void close()
        Closes the connection to the database.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable