package newquery;
import java.lang.*;
import java.util.Enumeration;
import java.sql.*;
import java.util.Vector;
import java.util.Hashtable;
public class dbManager
{String dbDriver;
String dbUrl;
String dbUser;
String dbPw;
String dbQuery;
String[] dbValues;
String[] dbNames;
Hashtable e;
Connection con;
public dbManager(Hashtable qinfo) throws SQLException,Exception,NullPointerException
{ this.dbDriver=qinfo.getStr("dbDriver");
this.dbUrl=qinfo.getStr("dbName");
this.dbUser=qinfo.getStr("dbUser");
this.dbPw=qinfo.getStr("dbPwd");
this.dbQuery=qinfo.getStr("dbQueries");
dbNames=spliter(dbQuery);//其中dbQuery保存了很多种查寻和修改方法名如lookup,add等!
//spliter函数是将这些方法名存入一个数组中,因为原来的dbQuery是一个字符串,各个方法名用“,”分开!
dbValues=new String[dbNames.length];
for(int i=0;i<dbNames.length;i++)
dbValues
=qinfo.getStr(dbNames);//getStr()是重载用来取特定字符串变量的值得!
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try{
Connection con=DriverManager.getConnection("jdbcdbc:Librarian");
if (con == null) {
System.out.println("Could not establish connection.");
} else
{
System.out.println("Connection successfully established.");
for(int i=0;
i<dbNames.length;i++)
{ try{
Query Q=new Query(dbNames,dbValues,con);
System.out.println(dbNames);
[black][blue]e.put(dbNames,Q);//就是这一步老是出错![/blue][/black]
}
catch(NullPointerException ne){}
}
}}catch(Exception e)
{e.printStackTrace();}
}
private String[] spliter(String dbQuery)
{if(dbQuery==null)
return null;
int j=0;int k=0;
Vector v=new Vector();
// char c;
StringBuffer sb1=new StringBuffer(dbQuery);
StringBuffer sb2=new StringBuffer();
for(int i=0;i<=sb1.length();i++)
{if(i<sb1.length()&&Character.isLetter(sb1.charAt(i)))
{continue;}
else
{
v.addElement(sb1.substring(j,i));
j=i+1;
}
}
return VectorToStringArray(v);
}
public Env getQueryStrings(Env qinfo) throws SQLException
{
String op=qinfo.getStr("dbOperation");
if(op==null)
return null;
Query Q=(Query)e.get(op);
return Q.getQueryStrings(qinfo);
}
/* public Resequence getQueryRow(Env e)throws SQLException
{
String op=qinfo.getStr("dbOperation");
if(op==null)
return null;
Query Q=(Query)e.getStr(op);
return Q.getQueryRow(qinfo);
}
*/
public void addQuery(String qNm,String qStr) throws SQLException
{e.put(qNm,new Query(qNm,qStr,con));
}
public void delQuery(String qNm)throws SQLException
{ Query Q=(Query)e.get(qNm);
if(Q==null)return;
Q.close();
e.remove(qNm);
}
public void close()throws SQLException
{try{
if(!e.isEmpty())
{Enumeration er=e.keys();
while(er.hasMoreElements())
{Query Q=(Query)er.nextElement();
Q.close();
}
}
if(con!=null)
con.close();
}catch(Exception ex){}
}
public String[] VectorToStringArray(Vector v)
{if (v==null||v.size()==0)
return null;
String[]R= new String[v.size()];
for(int i=0;i<R.length;i++)
R=(String)v.elementAt(i);
return R;
}
}