有没有搞过Class.forName的,求助

苦逼热狗

路边通讯社社长
VIP
注册
2002-10-12
消息
47,114
荣誉分数
2,376
声望点数
393
最近闲来无事,所以想研究以下dynamic class loading
例如我要实现一个generic database client.
支持mysql/postgresql, 在用户安装程序的时候选择使用mysql或者postgresql
private static final String[]dbtypes = {"test.db.servers.MySQL.MySQL","test.db.servers.PostgreSQL.PostgreSQL"};
private static final String[]qyerttypes = {"test.db.servers.MySQL.QueryObject","test.db.servers.PostgreSQL.QueryObject"};
private IDatabase db;
private IQueryObject query;
private Class DBClass;
private Class QueryClass;

try{
dbclass = Class.forName(dbtypes[1]);
db = (IDatabase) dbclass.newInstance();
querylass = Class.forName(querytypes[1]);
query = (IQueryObject) QueryClass.netInstance();
}catch(.....)....

问题是performance是不是会比直接建立对应的sql lib更慢,会慢多少呢?
 
Based on my understanding, that we could use singleton pattern to create the QueryObject in PostgreSQL package.
@Override
public IQueryObject getInstance(String obj) {
if (instance ==null) instance = new QueryObject();
this.obj = obj;
return instance;
}
However as a database, there is no way it only performs single task, which means we also need to take synchronization into account. That means we might need to create a pool of IQueryObject, use a getNextQueryObject to create a query. I wonder if i am on the right track, lol? Feel free to drop any comments :)
 
我回来自问自答了

package test.database;

import java.lang.reflect.Constructor;
import test.database.interfaces.IDatabase;
import test.database.interfaces.IQueryObject;

public class Database {
private static final int DATABASEINDEX = 1;
private static final String[]
DBTypes = { "test.database.servers.MySQL.MySQL",
"test.database.servers.PostgreSQL.PostgreSQL"
};
private static final String[]
QueryTypes = { "test.database.servers.MySQL.QueryObject",
"test.database.servers.PostgreSQL.QueryObject"
};

private IDatabase database;

public void init()
{
try
{
database = (IDatabase)Class.forName( DBTypes[ DATABASEINDEX ] ).newInstance();
}
catch( Exception e )
{
System.out.println( "Exception occurred: " + e.getMessage() );
e.printStackTrace();
}
}
public IQueryObject getQueryObject(String QueryOperation){

Constructor constr;
IQueryObject ret = null;

try {
constr = Class.forName( QueryTypes[ DATABASEINDEX ] ).getConstructor( String.class );
ret = (IQueryObject)constr.newInstance( QueryOperation );
//return (IQueryObject)Class.forName( QueryTypes[ 1 ] ).getConstructor(String.class).newInstance(start);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public void getSQL()
{
IQueryObject select = null,select2 = null;
database.initialize();
database.connect("localhost", 5432, "foo", "sqlogin", "");

select = getQueryObject(QueryOperations.SELECT);
select.set_select_list("*");
select.set_source("table1");
select.set_limit(1);

select2 = getQueryObject(QueryOperations.SELECT);
select2.set_select_list("*");
select2.set_source("table2");
select2.set_limit(1);

database.select(select);
database.select(select2);

database.close();

select.clear();
select2.clear();
select = null;
select2 = null;
}
public static void main(String[] argv) {
Database db = new Database();
db.init();
db.getSQL();
}

}
这个可以了
 
just realized that delegation in java is doable.

Class aClass = ...;
Object parameters[] = {String.class, int.class};
Method method = aClass.getMethod("method", parameters);
//call method(String, int)
Object arguments[] = {"Hello", new Integer(3)};
returnValue = method.invoke(aClassInstance, arguments);
 
............
给点建设性的回复好吗?
 
后退
顶部