0

I got to use MariaDB for my University Project. it's my first time doing it, so I dont't know well how to use and code JDBC Driver and mariaDB. Now I'm implementing the code in many places while looking at examples. As I see, All the examples seems to creating Statement and making connection by using "DriverManager.getConnection"

Now I have a question. I'm going to create a DBmanager Class that can connect, create tables, execute queries, and execute the code that updates data on tables in a single line.

I thought all the examples would run alone in one method and came from different places, so I could only try a new connection and create a code that would not close. But I have a gut feeling that this will be a problem.

Is there any way I can leave a connection connected at a single connection to send a command, and disconnect it to DB.disconnect()? And I'd appreciate it if you could tell me whether what I'm thinking is right or wrong.

The code below is the code I've written so far.

  • I am sorry if you find my English difficult to read or understand. I am Using translator, So, my English could not be display as I intended.
import java.sql.*;
import java.util.Properties;
public class DBManager {
    /*********INNITIAL DEFINES********/
    final static private String HOST="sumewhere.azure.com";//Azure DB URL
    final static private String USER="id@somewhere";//root ID
    final static private String PW="*****";//Server Password
    final static private String DRIVER="org.mariadb.jdbc.Driver";//DB Driver info

    private String database="user";


    /***************API***************/

    void setDB(String databaseinfo){
        database=databaseinfo;
    }

    private void checkDriver() throws Exception
    {
        try
        {
            Class.forName("org.mariadb.jdbc.Driver");
        }
        catch (ClassNotFoundException e)
        {
            throw new ClassNotFoundException("MariaDB JDBC driver NOT detected in library path.", e);
        }
        System.out.println("MariaDB JDBC driver detected in library path.");
    }

    public void checkOnline(String databaseinfo) throws Exception
    {
        setDB(databaseinfo);
        this.checkDriver();
        Connection connection = null;
        try
        {
            String url = String.format("jdbc:mariadb://%s/%s", HOST, database);

            // Set connection properties.
            Properties properties = new Properties();
            properties.setProperty("user", USER);
            properties.setProperty("password", PW);
            properties.setProperty("useSSL", "true");
            properties.setProperty("verifyServerCertificate", "true");
            properties.setProperty("requireSSL", "false");

            // get connection
            connection = DriverManager.getConnection(url, properties);
        }
        catch (SQLException e)
        {
            throw new SQLException("Failed to create connection to database.", e);
        }
        if (connection != null)
        {
            System.out.println("Successfully created connection to database.");
        }
        else {
            System.out.println("Failed to create connection to database.");
        }
        System.out.println("Execution finished.");
    }

    void makeCcnnection() throws ClassNotFoundException
    {
        // Check DB driver Exists
        try
        {
            Class.forName("org.mariadb.jdbc");
        }
        catch (ClassNotFoundException e)
        {
            throw new ClassNotFoundException("MariaDB JDBC driver NOT detected in library path.", e);
        }
        System.out.println("MariaDB JDBC driver detected in library path.");
        Connection connection = null;
    }

    public void updateTable(){}

    public static void main(String[] args) throws Exception {
        DBManager DB = new DBManager();
        DB.checkOnline("DB");
    }
}
JunYoung Yoon
  • 43
  • 1
  • 10
  • Include the connection close in a `finally` block. That pattern becomes really important when the app scales up to using a connection pool, so connections get returned to the pool. On the MariaDB side, the server will terminate abandoned connections when `wait_timeout` or `interactive_timeout` expires. Our design goal should be this feature is an extra backstop, handling connections abandoned by a crashed JVM. Apps running in the JVM should be explicitly closing connections, not abandoning them. (With a connection pool, app "open" and "close" connection will be "borrow: and "return" – spencer7593 Nov 27 '19 at 15:51

2 Answers2

1

For a studying project it's okay to give a connection from your DB Manager to client code and close it there automatically using try-with-resources construction.

Maybe you will find it possible to check Connection Pool tools and apply it further in your project or use as example (like HikariCP, here is a good introduction).

0

Read about Java try with resources. I think that this link could be usefull for your problem. JDBC with try with resources

David
  • 33
  • 8