I really liked Lashane's answer, I used the code to create the DataSource solution. I also reworked it to store a DataSource , not Connection , if you want to open several of them.
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; public class SignalDB { private static MysqlDataSource ds = null; public static MysqlDataSource getDataSource(String db_name) { if (ds == null) {
Then you can create connections using:
con = SignalDB.getDataSource("database_name").getConnection();
I added the ability to connect to another database every time, in some cases, like ours, this is what you need to do on the fly.
Hope this helps.
source share