Connect to two database

Hi There,
I have two database servers which is A and B and they are replicated.
If I connect to A is fail and I want to connect to B .. How do I modified my statement and connection .
thanks,
mhvan

// your connection strings - for example
String connectionStringForA = "jdbc:mysql://localhost/yourdatabase?user=youruser&password=XXXX";
String connectionStringForB = ".......";
// your connection driver - for example
String connectionDriver = "com.mysql.jdbc.Driver";
// Connection reference
Connection sqlCon = null;
try {
  // Load database driver
  Class.forName(connectionDriver);
  // Attempt to make connection with first connection string A
  sqlCon = DriverManager.getConnection(connectionStringForA);
// Class not found exception do your handling
catch (ClassNotFoundException cnfex) {}
// first connection failed try second
catch (SQLException sqlex) {
  try {
    // Attempt to make connection with second connection string B
    sqlCon = DriverManager.getConnection(connectionStringForB);
  // Class not found exception do your handling
  catch (ClassNotFoundException cnfex) {}
  // second connection failed do your handling
  catch (SQLException sqlex) {}
}rykk

Similar Messages

  • Connecting to two database simulteneously

    I am writing code to get data out of an existing customer database and put this data into a new database as part of a product update. I am told I can start one database server, and use it to get connections to both databases. My problem is that the database are in essence the same - they are just in different directories. My directory structure looks like this:
    c:\myapplication\my.db
    c:\old_myapplication\my.db
    To access the first database I do the following:
    // initialize the database driver
    String dbDriver = properties.getProperty("database_driver","com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource");
    String URL = "jdbc:sybase:Tds:127.0.0.1:2638";
    try {
      Class.forName(dbDriver);
    } catch(Exception e) {}
    originalDBConnection = DriverManager.getConnection(URL, "username","password");.....
    This works fine and I connect to the first database. My problem is that I can't figure out how to connect to
    the second database! I don't see where the path to the database is ever specified, and since both
    databases use the same driver it seems I need to use the same URL.

    Thanks fun_one, you were right, I did need to create two connections. I'm using jconnect...I'll post the final solution for posterity :-).
    To connect to two databases (and one server) using jconnect...
    1. When starting the database server, specify the names and paths of your databases. Here is my command:
    start dbsrv8.exe %2 %3 %4 -n %DBNAME% -c 32M -x tcpip -gkall -gdall -gx 30 c:\myapp\database\mydb.db -n db2 c:\myapp\database\mydb.db -n db1
    ....where db1 and db2 are the names of my databases.
    2. Then initialize the database driver as normal:
                // initialize the database driver
                String dbDriver = properties.getProperty("database_driver","com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource");
                try {
                    Class.forName(dbDriver);
                } catch(Exception e) {
                    e.printStackTrace(System.out);
                }3. Now, when getting a connection add the database name to the url
    //Connect to db1
    String urlOne = "jdbc:sybase:Tds:127.0.0.1:2638?ServiceName=db1";
    Connection db1connection = DriverManager.getConnection(urlOne, dbUserName, dbPassword);
    //Connect to db2
    String urlTwo = "jdbc:sybase:Tds:127.0.0.1:2638?ServiceName=db2";
    Connection db2connection = DriverManager.getConnection(urlTwo, dbUserName, dbPassword);I found the following webpage very useful:
    http://manuals.sybase.com/onlinebooks/group-sas/awg0703e/dbugen7/@Generic__BookTextView/47925

  • Hyperion- Can it Connect to two database at a time

    Hi All,
    I am new to Hyperion Reporting and Analysis. Kindly help me in the following Scenario.
    Consider two database 'A' and 'B'
    I want to create a report from tables from Database "A" and "B"
    How to do that.
    Kindly Help me out
    Thanks in advance
    Regards
    Gatha

    For Interactive Reporting there are a couple options.
    1. Us the OBIEE BI Server and model your semantic layer with the connections. The Product is designed for Federated Queries.
    2. Build within Client
    - Query 1 for Database A
    - Query 2 for Database B
    - Qyery 3 using local results build the data model from Query Results A and B - Joined appropriately
    3. Depending on the Database - SQLServer, Oracle, DB2 etc you could connect to multiple DB within one OCE - You will need to look at Mark Ostroff's tips and tricks guide from previous Hyperion Conference for how to do it.
    Hope this is Helpful
    Wayne Van Sluys
    TopDown Consulting

  • Connection Between Two Databases

    Hi,
    I have two databases called DBS1 and DBS2. Both are in one system. I need link between these two databases. I want a query as follows.
    select * from cust@DBS2; # from DBS1
    select * from temp_cust@DBS1; # from DBS2
    Is possible link between DBS1 and DBS2.
    Can any one tell me how to link these two databases each other.
    Best Regards,
    Nilopher.
    null

    Nilopher,
    You can connect to two different Oracle databases by creating a database link. When you say that both databases are in one system I assume you mean two databases on the same server. To create a database link you can do the following:
    While logged onto DBS1:
    create database link dbs2
    connect to username identified by password
    using 'dbs2.world'
    Username and password are the user and password you want to connect as in dbs2.
    If you want everyone to be able to access the database link you can say create public database link... otherwise only the user that created the database link can use it.
    This should work assuming the database server tnsnames.ora file looks like this:
    dbs2.world =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL= TCP)(Host= hostname)(Port= 1521))
    (CONNECT_DATA = (SERVICE_NAME = dbs2))
    If your databases are on two different servers you'd have to look at each one's tnsnames.ora file and make sure each one had an entry for the other's database.
    I believe the database link name must be the same as the SERVICE_NAME if the parameter GLOBAL_NAMES=TRUE, if it's FALSE you can name it something else. By default this parameter is set to TRUE so I called the database link in the example dbs2.
    You can then access data from dbs2 while logged into dbs1 exactly how you said:
    select * from cust@DBS2;
    You can then repeat the process to do the same thing on dbs2 to connect to dbs1.
    Hope this helps.

  • Connection with two databases

    Hi
    We have an application based on two databases (.dbf and mysql). Does exist only one way to question those two databases?
    Tanks

    Hi
    Yes! You can do this with ODBC.
    Class.forName("sun.jdbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:DB2SDN","user","pass");
    Connection con1 = DriverManager.getConnection("jdbc:odbc:mySQLDSN","user","pass");
    Statement st1 = con.createStatement();
    Statement st2 = con1.createStatement();
    ResultSet rs1 = st1.executeQuery(strQuery);
    ResultSet rs2 = st2.executeQuery(strQuery);
    Is that what you are looking for ?
    Thanks
    Swaraj

  • Connecting to two database instances using the same Toplink

    Hi,
    We are trying to create a process using BPEL where data from one database instance needs to be passed to the other. The process is compiling properly and we could deploy it in BPEL Process Manager. But when we try to run the process, the data is not getting inserted into the second instance. There is no error /exception being thrown at most of the time, but at times we are getting a 'Unique constraint error' eventhough there is no duplication of data.
    Can we connect to two different instances using the same toplink. How can we achieve this.
    Thanks in advance
    Pratheusha

    You need to use two different sessions with two different mapping descriptors

  • Connecting to two database instances from the same TopLink

    Hi,
    We are trying to create a process using BPEL where data from one database instance needs to be passed to the other. The process is compiling properly and we could deploy it in BPEL Process Manager. But when we try to run the process, the data is not getting inserted into the second instance. There is no error /exception being thrown at most of the time, but at times we are getting a 'Unique constraint error' eventhough there is no duplication of data.
    Can we connect to two different instances using the same toplink. How can we achieve this.
    Thanks in advance
    Pratheusha

    You need to use two different sessions with two different mapping descriptors

  • Using database link to connect to two databases

    Hi guys,
    Here's my question. I need to give a contractor Select rights on tables located in two schemas located on two different servers. One server is dev and one is prod. Let's say we have schema user: EMPLOYEE on dev and schema user: DEPARTMENT on prod. The outside contractor needs select rights on all tables in DEPARTMENT schema but only needs select on a few tables ( 4 to be exact) from EMPLOYEE schema.
    My initial train of thougth is: CREATE a user named: TEST (random name of course, just used for an example) for contractor on prod. Give user TEST select rights on all tables in DEPARTMENT schema.
    Create database link to dev server so user TEST can access EMPLOYEE schema.
    How do i go about limiting the user TEST via the database link to only view the 4 tables from EMPLOYEE instead of all teh tables?
    Should I also consider doing this scenario in reverse? Creating user TEST on dev, giving TEST rights to the 4 tables in EMPLOYEE, then creating the database link to connect to department schema on prod database??
    Any help will be appreciated. Thanks in advance.

    zephyr223 wrote:
    Hi guys,
    Here's my question. I need to give a contractor Select rights on tables located in two schemas located on two different servers. One server is dev and one is prod. Let's say we have schema user: EMPLOYEE on dev and schema user: DEPARTMENT on prod. The outside contractor needs select rights on all tables in DEPARTMENT schema but only needs select on a few tables ( 4 to be exact) from EMPLOYEE schema.
    My initial train of thougth is: CREATE a user named: TEST (random name of course, just used for an example) for contractor on prod. Give user TEST select rights on all tables in DEPARTMENT schema.
    Create database link to dev server so user TEST can access EMPLOYEE schema.
    How do i go about limiting the user TEST via the database link to only view the 4 tables from EMPLOYEE instead of all teh tables? Only grant them access to those four tables from EMPLOYEE. You have to explicitly grant privileges anyways, and since there is no GRANT SELECT ON <SCHEMA> ability it should not be that difficult.
    Should I also consider doing this scenario in reverse? Creating user TEST on dev, giving TEST rights to the 4 tables in EMPLOYEE, then creating the database link to connect to department schema on prod database?? Are you creating database links as the EMPLOYEE and DEPARTMENT users? If so, I would caution against that. Create a "current user" database link for TEST. I think you can manage privileges better that way.
    HTH!
    HTH!

  • Can I connected to two Database in jdbc page?

    I need to run two queries in my project but it has two different databases.

    Yes.
    You have to load the appropriate driver(s) and use two different Connection objects, but as long as the 2 DBs have different URLs, it all works just fine.

  • Connecting to two databases

    I have two seperate databases in SQL Server. They are both in different folders and have different passwords. I need to use two different beans. How do I connect to both of the databases to get data out of both of them?
    This is what I have:
    <jsp:useBean id="db" scope="request" class="###" />
    <jsp:setProperty name="db" property="dbDriver" value="###" />
    <jsp:setProperty name="db" property="dbURL" value="##" />
    Can I just make another one of these for the other database? Just rename db to db1 for the second instance and dbURL, dbDriver to db1?
    Will this work?

    Ofcourse it will work! Try giving different beanid. In any case while reffering you will always refer it with usebeanid.xyz notation. So it won't commit any problems...
    if it does, plz let me know

  • Connecting between two database of various version

    Hi,
    we are using apex 3.0.1 and 3.2.1 which has 9i and 10g database .
    We have different application in different servers.
    Now i want access a table in apex version 3.2.1 from apex version 3.0.1 via a trigger.
    what are the process i have to perform to connect the database
    Thanks & Regards,
    Gomathi

    >
    via a trigger
    >
    Sounds like a very bad idea - what exactly are you trying to achieve?
    Either way, you need to look up database links. Here's the docs for 10g...
    http://www.stanford.edu/dept/itss/docs/oracle/10g/server.101/b10759/statements_5005.htm
    Cheers
    Ben

  • Connection to two databases

    hello'
    Need help on retrieving data into a single  from tables on  separate databases each has its own username and password .
    Thanks

    Try connecting to 3 different databases from crystal and add 3 tables and join them if they have any fields to join. If there is no link between those tables then try inserting a sub report for each table from each database.
    Regards,
    Raghavendra

  • Run two Database in single server

    Hi...
    I want to run two database into single server. For that I had created one another database.
    But now I am not able to connect with two database at the same time.
    When I am going to connect to another database i have to shutdown the first one.
    Is it possible to connect and work on two database at the same time?
    Thanks
    pratik

    You should have a good enough to have two or more instances running on the same machine.
    The performance of a single instnace will always be better compared to having two or more instances on the same machine.
    Is it possible to connect and work on two database at the same time?Yes
    Here is an example
    $ ps -ef |grep pmon
    ora101AS 548928 1 0 Aug 06 - 0:31 ora_pmon_orcl
    ora102DB 552974 1 0 Aug 06 - 0:36 ora_pmon_TEST
    ora102DB 1380412 1 0 00:58:49 - 0:04 ora_pmon_LIVE
    testuser 1613946 1691868 0 10:46:11 pts/2 0:00 grep pmon
    Single machine having multiple instances
    $ ps -ef |grep pmon
    ora102DB 4086 1 0 Aug 06 - 1:39 ora_pmon_LIVE10
    testuser 32848 31172 0 10:46:24 pts/0 0:00 grep pmon
    Single machine having a single instance
    The second machine performance is much better compared to the first machine that has mutiple instances.
    Normally CREATE DATABASE is used by advance users while DBCA is relatively easy to use.
    Adith

  • I want to connect two database in Oracle

    Dear All
    I want to connect two database in Oracle ver-10.2.0.1.0 ,And fetch data from 2nd table.
    OS - XP Pro.
    Regards,
    Parwez

    Configure remote DB in TNS first.
    Create DB link.
    CREATE {PUBLIC} DATABASE LINK "DBLINKNAME"  CONNECT TO "remoteuser" IDENTIFIED BY "remotepwd" USING 'REMOTEDBTNSALIAS';Query remote tables using
    select * from remotetable@DBLINKNAME;

  • Connect two database in runtime

    I have a application, which will set the JDBC connection once the tomcat load. Then i can get the connection each time. But there are another function in the application, which will insert some data into another database. But I don't know how to load two database connection once the tomcat load or switch to another database base. Thanks for you help.
    In the context.xml. the current setting as belows
    <?xml version="1.0" encoding="UTF-8"?>
    <Context reloadable="true" path="/">
    <Resource auth="Container"
                   driverClassName="oracle.jdbc.OracleDriver"
                   maxActive="15"
                   maxIdle="60"
                   maxWait="8000"
                   name="jdbc/dbdatasource"
                   username="xxx"
                   password="xxx"
                   type="javax.sql.DataSource"
                   url="jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.1)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.2(PORT = 1521))(LOAD_BALANCE = yes))(CONNECT_DATA =(SERVICE_NAME = toc)))"/>
    </Context>
    And when the application get connection, will use the following coding.
    db = new DBAccess(this.getClass());
    public DBAccess(Class sInClass) throws Exception{
    sCallerClassName = sInClass.getName();
    connectDB();
    private void connectDB() throws Exception{
    String sDbName;
    String sDbUser;
    String sDbPasswd;
              try {
              Context initCtx = new InitialContext();
              Context envCtx = (Context) initCtx.lookup("java:comp/env");
              DataSource ds = (DataSource) envCtx.lookup("jdbc/dbdatasource");
              this.oCon = ds.getConnection();
    oSystemLog.debug(" ----- Class " + sCallerClassName + " connection started ----- ");
    } catch (Exception e) {
    oSystemLog.error("",e);
    throw e;
    }

    Configure another Resource entry with a different JNDI name (yours is called "jdbc/dbdatasource") in Tomcat and create a corresponding mapping in your web application's deployment descriptor (the web.xml file).

Maybe you are looking for