Good Evening

i hope i will be clear and fully details.
i have created a web service project on netbeans using soap technology.
i am using xampp mysql to create 5 different simple hotel databases.
from my project i am connecting with jdbc connector succesfully only to 1 database.
that i am trying to do is a broker that manages these 5 databses with jquerrys and booking rooms

i tried to connect directly to 5 dbases but i cannot. what i did is just copy pasting the connect code and rename the database name as it seems here on the constructor.

public hotels() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel", "root", "");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/polaris", "root", "");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/palace", "root", "");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

when i run this the system connects only to the last database(palace).
what can i do in order to connect directly to all databases?

Is that the same connection variable you are using for all of them? If yes, then you are effectively throwing away the connection retrieved. The way you should test is: retrieve the connection for a given database and fire some query specific to that database which will ensure that you are indeed connecting to the database in consideration.

commented: Well said. +7

Is that the same connection variable you are using for all of them?

Yes. Use different variables. Or method calls. Or a loop. Or something.

Also, you really only need to do 'Class.forName("com.mysql.jdbc.Driver");' once, at the beginning.

For everyone's reference, if using JDK 6 and a JDBC library built for Java 6 runtime, just having the JDBC JAR in the classpath is enough to load the driver, no more `Class.forName` required! This is because of the ServiceLoader facility publicly introduced in JDK 6 which allows "service providers" (in this case JDBC driver vendors) to provide an implementation class for a particular service (in this case java.sql.Driver interface).

The way this works is:

When your application code first refers to the DriverManager.getConnection() method, the static method loadInitialDrivers() queries the ServiceLoader API for any java.sql.Driver implementations present in the classpath in the form of META-INF/services entries and loads the class by name if present. If it finds it, a Class.forName method is invoked to load the service implementation.

To verify, open up the JDBC driver for MySQL and navigate to the META-INF/services directory. This directory should contain a UTF-8 encoded text file having the name 'java.sql.Driver'. The contents of the text file would be the name of the implementation class, in our case, com.mysql.jdbc.Driver. This is the class which is loaded and typically would contain the code to register with the DriverManager as we can see from the source.

For everyone's reference, if using JDK 6 and a JDBC library built for Java 6 runtime, just having the JDBC JAR in the classpath is enough to load the driver. This is because of the ServiceLoader facility publicly introduced in JDK 6 which allows "service providers" (in this case JDBC driver vendors) to provide an implementation class for a particular service (in this case java.sql.Driver interface).

The way this works is:

When your application code first refers to the DriverManager.getConnection() method, the static method loadInitialDrivers() queries the ServiceLoader API for any java.sql.Driver implementations present in the classpath in the form of META-INF/services entries and loads the class by name if present. If it finds it, a Class.forName method is invoked to load the service implementation.

To verify, open up the JDBC driver for MySQL and navigate to the META-INF/services directory. This directory should contain a UTF-8 encoded text file having the name 'java.sql.Driver'. The contents of the text file would be the name of the implementation class, in our case, com.mysql.jdbc.Driver. This is the class which is loaded and typically would contain the code to register with the DriverManager as we can see from the source.

i really dont understand how to do this? anyone could help?

Which specific part do you have trouble understanding?

You just need to put the MySQL JDBC driver in your classpath, remove `Class.forName()` from your code and it should automatically work. Nothing more needed from your side.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.