Connecting database help needed with driver


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Connecting database help needed with driver

  1. #1
    Join Date
    Dec 2004
    Location
    france
    Posts
    35

    Connecting database help needed with driver

    Hi everybody
    here's my code i just can't foind out why it wouldn't connect to database

    thanks in advance





    import java.sql.*;
    import javax.sql.DataSource;
    import java.io.*;
    import javax.naming.InitialContext;

    class Datasource{

    public static void main(String[] lalama)throws IOException{

    System.out.println(" Enter the database file name ");
    String dsn="jdbcdbc:"+Driverdemo.in.readLine();


    try{

    System.out.println( "Initializing the naming context");


    InitialContext init=new InitialContext();


    System.out.println("Looking up "+dsn);

    DataSource source=(DataSource)init.lookup(dsn);


    System.out.println( " Establishing.. a connection ");

    Connection con=source.getConnection();


    System.out.println(" Connected to "+con.getCatalog());


    }catch(SQLException s){

    System.out.println(s+" Just occured ");
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    Compil and run!

  2. #2
    Join Date
    Jan 2005
    Location
    London
    Posts
    10
    Within your code here :

    DataSource source=(DataSource)init.lookup(dsn);

    System.out.println( " Establishing.. a connection ");

    the dsn is set to jdbc:xxxx
    Whenever I use a JNDI context to lookup a data source I use the following :

    InitialContext ctx = new InitialContext();
    ctx.lookup(java:comp/env/jdbc/*DSNameConfiguredInServer*);

    Also log out your ds once looked up to ensure it is not looking up a null object:

    e.g:

    DataSource source=(DataSource)init.lookup(dsn);

    System.out.println( " Establishing.. a connection using ds="+source);


    if source is null then it has not looked it up.

    Hope that helps

    M

  3. #3
    Join Date
    Jan 2005
    Location
    London
    Posts
    10
    Also - are you using J2C Authentication aliases and if so have you set this security object up and assigned it to your datasource as this is the mistake I always make. If this is running on a J2EE server such as Websphere you may need to restart the node agents to pick up all data source settings.

    If these don't help post the error you receive and i will see if i can help

  4. #4
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    thanx for your interrest it ain't working

    what you suggest if i were to connect to microsoft access database
    thanx again
    Compil and run!

  5. #5
    Join Date
    Jan 2005
    Location
    London
    Posts
    10
    To be honest I have never connected to access only sybase/oracle/mysql/sqlserver but I would think all you would need to do is find the MS access database jar file and create the datasource as an access one like any other db connection - we connect to sql server using the class com.microsoft.jdbcx.sqlserver.SQLServerDataSource using jars : msbase.jar/mssqlserver.jar/msutil.jar but having a quick look I could not see any obvious access classes.

    sorry i can't be more helpful

    M

  6. #6
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    anyway i wanna thanx you again for your support
    i'll work on that eventually let you know how it is goin
    take care

    feel free not to answer this one
    Compil and run!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center