Java中的MySQL进行自动不需要的查询

时间:2018-08-14 02:08:15

标签: java mysql

我有一段简单的代码,可以使用以下两个对象在Java上运行MySQL查询:

    package dbConn;

import java.sql.*;

public class DBAccess{

    private Connection conn;
    private Statement st;

    DBAccess(String driver, String connString, String user, String password) {

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(connString, user, password);
            st = conn.createStatement();
        } catch (ClassNotFoundException e) {
            System.out.println("Driver class not found.");
        } catch (SQLException e) {
            System.out.println("Unable to get connection.");
        }

    }

    public void executeSQL(String stmt) {

        try {
            st.execute(stmt);
        } catch (SQLException e) {
            System.out.println("Error executing query.");
        }

    }

    public ResultSet executeSQL(String stmt, boolean returns) {
        ResultSet rs = null;

        try {
            rs = st.executeQuery(stmt);
        } catch (SQLException e) {
            System.out.println("Error executing query.");
        }

        return rs;
    }

    public void setAutocommitFalse() throws SQLException {
        conn.setAutoCommit(false);  
    }

    public void setAutocommitTrue() throws SQLException {
        conn.setAutoCommit(true);
    }

    public void commitTransaction() throws SQLException{
        conn.commit();
    }

    public void rollbackTransaction() throws SQLException{
        conn.rollback();    
    }

    public void closeConn() {
        try {
            conn.close();
        } catch (SQLException e) {
            System.out.println("Error closing connection.");
        }

    }   

}

然后这个:

package dbConn;

import java.io.*;
import java.util.TreeMap;
import java.util.Properties;

public class Connector{

    private DBAccess conn = null;

    public Connector() {

    }

    public DBAccess getConector() {

            TreeMap param = getParams();

            if (conn == null) {         
                conn = new DBAccess("com.mysql.jdbc.Driver","jdbc:mysql://" + param.get("dbhost") + ":" + param.get("dbport") + "/" + param.get("dbschema") + "?useSSL=false",
                                    param.get("dbuser").toString(),param.get("dbpassword").toString());
            }

            return conn;
    }

    private TreeMap getParams() {

        TreeMap<String, String> parametros = new TreeMap<>();
        Properties prop = new Properties();

        try {
            InputStream input = new FileInputStream(System.getProperty("user.dir") + "/db.properties");
            prop.load(input);
        } catch (FileNotFoundException e) {
            System.out.println("Properties file not found.");
        } catch (IOException e) {
            System.out.println("Unable to load input file to properties object.");
        }

        parametros.put("dbhost",prop.getProperty("dbhost"));
        parametros.put("dbport",prop.getProperty("dbport"));
        parametros.put("dbschema",prop.getProperty("dbschema"));
        parametros.put("dbuser",prop.getProperty("dbuser"));
        parametros.put("dbpassword",prop.getProperty("dbpassword"));

        return parametros;
    }

}

为了运行查询,我就这样做:

String query = "call get_by_id('" + type.getTableName() + "','" + type.getIdField() + "','" + id + "');";
DBAccess conn = new Connector().getConector();
ResultSet rs = conn.executeSQL(query, true);

然后循环遍历rs。这很好。但是,当我查看一般日志时:

enter image description here

连接和退出之间的所有时间大约需要一秒钟。我想减少时间。有没有办法让我的代码不运行SET,SHOW和Connector语句?它是强制性的吗?我知道一秒钟并不多,但是我经常调用那些存储过程,并且在运行应用程序时,如果我需要多次运行,则滞后不是很友好。

这与我使用的驱动程序有关吗?我可以在连接字符串中添加一些内容以避免这种情况吗?

谢谢!

0 个答案:

没有答案