package DWR;
import java.io.File;
import java.net.URLDecoder;
import java.sql.* ;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 12-8-10
* Time: 下午2:46
*/
public class DBclass {
// public static void main(String args[]) throws SQLException {
public List getResult(Map formMap) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
String str =Thread.currentThread().getContextClassLoader().getResource("") + "";
String strpath = str.substring(6, str.lastIndexOf("WEB-INF")) ;
strpath=URLDecoder.decode(strpath, "UTF-8") ;
System.out.print(strpath);
// 路径示例 file:/F:/TestOut/web/WEB-INF/classes/
strpath = strpath+"CORBA_DB/" ;
strpath = strpath + getDBfilename(strpath);
strpath = strpath+".mdb" ;
//System.out.print(strpath);
Class.forName("net.sf.log4jdbc.DriverSpy");
String strDBQ = "DBQ="+strpath;
String dburl ="jdbc:log4jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};";//此为NO-DSN方式
dburl = dburl+ strDBQ;
//String dburl ="jdbc:odbc:odbcName";//此为ODBC连接方式
conn = DriverManager.getConnection(dburl);
stmt=conn.createStatement();
String sqls = formMap.get("sqls").toString();
rs = stmt.executeQuery(sqls);
ResultSetMetaData data=rs.getMetaData();
while(rs.next()){
Map map = new HashMap();
for(int i = 1 ; i<= data.getColumnCount() ; i++){
//获得指定列的列名
String columnName = data.getColumnName(i);
//获得指定列的列值
String columnValue = rs.getString(i);
map.put(columnName,columnValue);
}
list.add(map);
}
} catch (Exception e) {
System.out.print(e.getMessage());
list=null;
}finally {
try{
if(rs != null) {
rs.close();
}
if(stmt != null) {
stmt.close();
}
if(conn!=null){
conn.close();
}
} catch (Exception e){
e.printStackTrace();
}
}
return list;
}
public String getDBfilename(String PATH){
Set<String> fileSet = new HashSet<String>();
File[] files = new File(PATH).listFiles();
String str ="";
for (File file : files) {
// 由于Set的元素是不重复的,不用做重复判断,直接放入
fileSet.add(getPrefixFileName(file.getName()));
}
// 由于Set允许null,所以要去除
fileSet.remove(null);
//迭代的方法
Iterator it = fileSet.iterator();
//这个set是否有下一个
while(it.hasNext()){
//有就把下一个取出来
str = (String) it.next();
}
return str ;
}
public static String getPrefixFileName(String fileName) {
if (fileName == null || fileName.length() == 0) {
return null;
}
int index = fileName.indexOf(".");
if (index < 0) {
// 不含SEPARATOR的文件名
return null;
}
// 从文件名开头到SEPARATOR的位置,截取
return fileName.substring(0, index);
}
}