MariaDB是自动断开连接还是我必须手动断开连接?

时间:2019-11-27 15:25:39

标签: java mysql jdbc mariadb

我必须在我的大学项目中使用MariaDB。 这是我第一次这样做,所以我不太了解如何使用和编码JDBC Driver和mariaDB。 现在,在查看示例的同时,我正在许多地方实现代码。 如我所见,所有示例似乎都使用“ DriverManager.getConnection”来创建Statement并建立连接

现在我有一个问题。 我将创建一个DBmanager类,该类可以连接,创建表,执行查询以及执行用于更新表中数据的代码。

我认为所有示例都将在一种方法中单独运行并且来自不同的地方,因此我只能尝试建立新连接并创建无法关闭的代码。但是我有一种直觉,认为这将是一个问题。

有什么方法可以让一个连接保持连接状态,以发送命令并将其断开与DB.disconnect()的连接?如果您能告诉我我的想法是对还是错,我将不胜感激。

下面的代码是我到目前为止编写的代码。

  • 对不起,如果您发现我的英语难以阅读或理解。我正在使用翻译器,所以我的英语无法按预期显示。
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");
    }
}

2 个答案:

答案 0 :(得分:1)

对于正在研究的项目,可以将您的数据库管理器与客户端代码建立连接,然后使用try-with-resources construction在其中自动将其关闭。

也许您会发现可以检查连接池工具并将其进一步应用到项目中或用作示例(例如HikariCP,这里是good introduction)。

答案 1 :(得分:0)

了解 Java试用资源。我认为该链接可能对您的问题有用。 JDBC with try with resources