This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and Relational Databases and the fly likes connection object question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "connection object question" Watch "connection object question" New topic
Author

connection object question

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

I am trying to create an instance variable of the Connection object to utilize in a JUnit test I am trying to compile. However I am getting the following error:

Cannot instantiate the type Connection, since it is not a concrete class

I am just trying to do:

Connection con = new Connection();

Is there a way to get around this to get this instance variable created?

Any help or direction would be appreciated. I've tried googling for an answer but I could not find anything that I could understand as to what they were trying to accomplish.

Thanks!!
[ August 04, 2005: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62488
    
  68

That is not the way to create a connection. You need to find yourself a JDBC tutorial.

Perhaps this one.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

Bear,

Thanks for the reply. I am already making a connection as below:

public Connection getConnection(){
// Make connection to a SQL Server 2000 database.
Connection con = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+ HOST_NAME +":1433;DatabaseName="+ DB_NAME +";User="+ USER_NAME +";Password="+ PASSWORD +"");
}
catch (Exception e) {
fail("Get Connection getConnection threw an exception: " +(e.getMessage()));
}
return con;
}

What I want to do is setup an INSTANCE VARIABLE for con instead of a local variable. I am wondering if I can do that from the connection object?

Sorry that I was not clear.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62488
    
  68

What I want to do is setup an INSTANCE VARIABLE for con instead of a local variable.


I have no idea why you think that makes any difference. How you obtain a connection has no bearing on what kind of variable you store it in.
Yilmaz Mete
Greenhorn

Joined: Dec 23, 2003
Posts: 28
Melinda,

part actually does the magic. The static block in SQLServerDriver Class calls DriverManager Class' static method register and registers itself.

Say 5 such classes registered themselves separately to the DriverManager; then depending on the getConnection() method's URL Parameter : that specific Driver Class is called by the DriverManager.


Actually a Driver class might trick the DriverManager and can handle the connections to other RDBMS's also. But it must have written the logic to talk to that specific DB of course.
[ August 04, 2005: Message edited by: Yilmaz Mete ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connection object question