Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connection object question

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not the way to create a connection. You need to find yourself a JDBC tutorial.

Perhaps this one.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic