Organizational Research By

Surprising Reserch Topic questions - Question:How to keep connection alive in java


I am working on my first Java Project with MySQL. I have one function that gets called every time I get data back from my data source. This function should save a new line to my MySQL database. See the code here:
import java.sql.*;
import java.util.Properties;
 * @author jeffery
public class SaveToMysql {
    // The JDBC Connector Class.
    private static final String dbClassName = "com.mysql.jdbc.Driver";
    private static final String CONNECTION = "jdbc:mysql://localhost/test";
    static public String test(int reqId, String date, double open, double high, double low,
                                        double close, int volume, int count, double WAP, boolean hasGaps){
        if (date.contains("finished")){
            return "finished";
        // Class.forName(xxx) loads the jdbc classes and
        // creates a drivermanager class factory
        }catch ( ClassNotFoundException e ) {
        // Properties for user and password. Here the user and password are both 'paulr'
        Properties p = new Properties();
        // Now try to connect
        Connection conn;
            conn = DriverManager.getConnection(CONNECTION,p);
        }catch(SQLException e){
            return e.toString();
        PreparedStatement stmt;
            stmt = conn.prepareStatement("insert into dj_minute_data set symbol = (select ticker from dow_jones_constituents where id = ?), "
                    + "date = str_to_date(?,'%Y%m%d %H:%i:%s')" +
            ", open = ?" +
            ", high = ?" +
            ", low = ?" +
            ", close = ?" +
            ", volume = ?" +
            ", adj_close = ?");
            stmt.setInt(1, reqId);
            stmt.setString(2, date);
            stmt.setDouble(3, open);
            stmt.setDouble(4, high);
            stmt.setDouble(5, low);
            stmt.setDouble(6, close);
            stmt.setDouble(7, volume);
            stmt.setDouble(8, WAP);
        }catch (SQLException e){
            return e.toString();
        }catch (SQLException e){
            return e.toString();
        return stmt.toString();
As you all can see this function test is in its own class, called SaveToMysql. To call this function, I import the class into a different class, and use this syntax:
msg = SaveToMysql.test(reqId, date, open, high, low, close, volume, count, WAP, hasGaps);
The msg then get output to the screen. Showing either error message or success.
This function may be called many times rapidly in a short time period. I know I should not have to re-open my connection with the MySQL server every time the function gets called. How would I change this so that the 1 MySQL connection stays open for every call to the function.

asked Sep 13, 2013 in MYSQL by anonymous
edited Sep 12, 2013
0 votes

Related Hot Questions

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.