B4J Question MySQL Multiple databases?

techknight

Well-Known Member
Licensed User
Longtime User
Is there an easy way to switch databases? I am working between 3 different databases all of which a single MySQL User has access to.

But, I dont see an easy way to do this because of the database being in the "URL".

Is there an easy way to do this? or do I have to have to keep calling pool.Initialize over and over again? or can I just call pool.Initialize once for each DB, but then how to select???

Right now I have a Pool.Initialize in each of my subroutines that deal with each different DB but it seems slow and inefficient. I did manage to get SSL working and that was a complete nightmare to accomplish, but I did.

I am lost here, any ideas?
 
This has nothing to do with code and everything to do with maintaning and switching to different databases on the same JDBC connection.

Thats what I am trying to accomplish. As far as the code that I have, I dont really have any except for connecting to the ONE server, with ONE database.
 
Upvote 0
Don't supply the database name in the URL of the connection (just the server). You have to then prefix your table names with the database name, sorta like:

SELECT db.table.column FROM db.table WHERE db.table.anothercolumn > 5

You should also then be able to do JOINS across databases (see https://stackoverflow.com/a/12646901).
 
Upvote 0
Perfect. Some of this stuff is slowly starting to come back to me. I havent really touched MySQL since 2004 or thereabouts, so as you can imagine I am a little rusty. I had to write a PHP guestbook my senior year of high school as a capstone, the same year I got my iNet+ certification which might as well be useless as I cant remember much from that time period, not to mention web technology has changed drastically since then.
 
Upvote 0
Just to remind you about the PHP connection, you might find this template useful. You can also use the Include statement for safety.

Code:
<?php

    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "database";
   
$conn = mysqli_connect("localhost", "username", "password");

if (!$conn)

echo("Successfully connected to " . $dbname);

{
   die("Unable to connect to " . $dbname);
}

?>
 
Upvote 0
Yea I know PHP, I have it fundamentally implemented for doing small things here and there.

The method OliverA proposed actually worked, I gave it a shot yesterday.
 
Upvote 0
Back
Top