DSN-Less JDBC connections

I have 2 SQL Server 2000 Databases. One is the master database of user accounts . In one of the tables in that database is a field that contains a connection string that points to the users database which could be anywhere on any type of database.

How do I create connection objects in Java Using JDBC without using a dsn.
Who is Participating?
skinsellaConnect With a Mentor Commented:
Just done this and it works (2 differnet ways):


String connectionString = "jdbc:odbc:Driver={SQL Server};Server=;Database=MyDatabase;Uid=username;Pwd=password;";

Connection DBConn = DriverManager.getConnection(connectionString);



String connectionString = "jdbc:odbc:Driver={SQL Server};Server=;Database=MyDatabase;";

Connection DBConn = DriverManager.getConnection(connectionString, "username", "password");
You need a Type 4 driver ( A Jdbc Driver specifically for the database you are trying to connect to)

click the following link:
If that doesn't work go to Microsoft's home page and type this in the search box: Microsoft SQL Server 2000 Driver for JDBC

Download and install. It comes with instructions on how to create the connection string. This needs to be on any machine that the applet/application/or servlet will be running on.

Quick Side note. It seems to me that you are going to have to do a little bit of planning because this driver will only work with MS SQL Server not Oracle not MySQL just MS SQL Server. So you're going to need a field in your db table saying what kind of db the connect string is for.

Hope this helps

cvcarsonAuthor Commented:
This project was originally for c++ server side code and I was using ADO. But my inexperience with pointers made me migrate to Java (I like much better by the way).

It's no problem adding a field to the db. All of our customers have are on our SQL Server 2000 Db's I was just thinking about the future but for now this will work.

I will test and award points if it works.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

I only tried Oracle thin connection...so far
If you don't want to go down the Type 4 route, you could  use the ODBC JDBC Bridge assuming there is a SQL Server ODBC Driver locally (there should be).

Anyway, here's an example for MS-Access, you just need to amend the connection string and driver name for SQL Server.

String db = "jdbc:odbc:Driver={Microsoft Access(*.mdb)};DBQ=C:/foo/foobar.MDB";

Connection DBConn = DriverManager.getConnection(db,"","");

You will have to replace the [Microsoft Access *.mdb] part with the ODBC driver name exactly as it appears in the Driver column of the ODBC DataSource Configuration on your windows machine.

If this is unclear, please repost...


cvcarsonAuthor Commented:
I like this idea. But can you give me an example of SQL Server?
cvcarsonAuthor Commented:
I will try it.
If you face a problem, try this:

Class myClass = Class.forName(....)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.