Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Make class Polymorphic??  RSS feed

 
d frazier
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out a little polymorphic riddle...
I have a class that returns a connection from my Oracle database and one that returns the connection to my mySQL database.
Each takes slightly different parameters,
rather than maintain 2 classes, I'd like to figure out how to combine both...I'll take any suggestions.
Thanks
Dane
I have a class (for mySQL) as follows (the Oracle version is nearly identical):
/*
readProps is a class that reads vars from the properties file
*/
package makeConMySQL;
import java.io.*;
import java.sql.*;
import readProps.*;
public class makeConMySQL
{
readProps rp = new readProps();
Connection myCon = null;
String uid = rp.getUID();
String pwd = rp.getPSWD();
public Connection getmyCon() { return myCon;}

public makeConMySQL()
{
try
{
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
String url = "jdbc:mysql://localhost/chi_Advmnt";
myCon = DriverManager.getConnection(url, uid, pwd);
}
catch (SQLException e)
{
System.out.println("SQL Exception Thrown during Connection! Err: " + e.getMessage());
}
}
}
 
Tighe Fagan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's three class templates that would do the
trick. Basically you introduce a base class that
contains the common code, and 2 subclasses (e.g. sqlCon $ oracleCon) that contain only the specialized code:

public class Base
{
public void doWork()
{
doCommonCodeWork();
doSpecializedCodeWork();
}

public void doCommonCodeWork()
{
// Put your common code here
}
public abstract void doSpecializedCodeWork();
}
public class Derived1 extends Base
{
public void doSpecializedWork()
{
// Do your SQL specific stuff here
}
}
public class Derived2 extends Base
{
public void doSpecializedWork()
{
// Do your Oracle specific stuff here
}
}
[This message has been edited by Tighe Fagan (edited August 09, 2001).]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!