-
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="jdbc dbc:"+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!
-
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
-
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
-
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!
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks