|
@ -3,7 +3,11 @@ package com.yihu.ehr.ws;
|
|
|
import com.yihu.ehr.dbhelper.jdbc.DBHelper;
|
|
|
import com.yihu.ehr.model.DataSource;
|
|
|
import com.yihu.ehr.ws.inf.ISQLWebService;
|
|
|
import net.sf.json.JSONArray;
|
|
|
import net.sf.json.JSONObject;
|
|
|
import net.sf.json.xml.XMLSerializer;
|
|
|
import org.apache.commons.dbcp2.BasicDataSource;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
|
import javax.jws.WebService;
|
|
|
|
|
@ -12,16 +16,73 @@ import javax.jws.WebService;
|
|
|
*/
|
|
|
@WebService(endpointInterface = "com.yihu.ehr.ws.inf.ISQLWebService", serviceName = "testWS")
|
|
|
public class SQLWebService implements ISQLWebService {
|
|
|
public static DBHelper db = null;
|
|
|
public static JdbcTemplate jdbcTemplate = null;
|
|
|
|
|
|
@Override
|
|
|
public String excuteSQL(String sql) {
|
|
|
if (db == null) {
|
|
|
DBHelper dbTemp = new DBHelper();
|
|
|
DataSource source = dbTemp.load(DataSource.class, "select * from system_datasource");
|
|
|
db = new DBHelper("new", source.getConfig());
|
|
|
}
|
|
|
try {
|
|
|
if (jdbcTemplate == null) {
|
|
|
//jdbc:oracle:thin:hos/hos@//172.19.103.71:1521/orcl
|
|
|
//jdbc:mysql://172.19.103.71:1521/orcl?user=hos&password=hos&useUnicode=true&characterEncoding=UTF-8
|
|
|
//jdbc:sqlserver://172.19.103.71:1521/orcl?user=hos&password=hos
|
|
|
DBHelper dbTemp = new DBHelper();
|
|
|
DataSource source = dbTemp.load(DataSource.class, "select * from system_datasource");
|
|
|
String config = source.getConfig();
|
|
|
BasicDataSource dasicDataSource = new BasicDataSource();
|
|
|
if (config.contains("oracle")) {
|
|
|
//oracle数据库
|
|
|
dasicDataSource.setUrl(source.getConfig());
|
|
|
dasicDataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
|
|
|
String[] s1 = config.split("@");
|
|
|
String[] s2 = s1[0].split(":");
|
|
|
String[] s3 = s2[3].split("/");
|
|
|
dasicDataSource.setUsername(s3[0]);
|
|
|
dasicDataSource.setPassword(s3[1]);
|
|
|
System.out.println("config:" + config);
|
|
|
System.out.println("username:" + s3[0]);
|
|
|
System.out.println("password:" + s3[1]);
|
|
|
} else if (config.contains("mysql")) {
|
|
|
//mysql数据库
|
|
|
dasicDataSource.setUrl(source.getConfig());
|
|
|
dasicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
|
|
String[] s1 = config.split("\\?");
|
|
|
String[] s2 = s1[1].split("&");
|
|
|
dasicDataSource.setUsername(s2[0].split("=")[1]);
|
|
|
dasicDataSource.setPassword(s2[1].split("=")[1]);
|
|
|
System.out.println("config:" + config);
|
|
|
System.out.println("username:" + s2[0].split("=")[1]);
|
|
|
System.out.println("password:" + s2[1].split("=")[1]);
|
|
|
} else if (config.contains("sqlserver")) {
|
|
|
//sqlserver数据库
|
|
|
dasicDataSource.setUrl(source.getConfig());
|
|
|
dasicDataSource.setDriverClassName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
|
|
|
String[] s1 = config.split("\\?");
|
|
|
String[] s2 = s1[1].split("&");
|
|
|
dasicDataSource.setUsername(s2[0].split("=")[1]);
|
|
|
dasicDataSource.setPassword(s2[1].split("=")[1]);
|
|
|
System.out.println("config:" + config);
|
|
|
System.out.println("username:" + s2[0].split("=")[1]);
|
|
|
System.out.println("password:" + s2[1].split("=")[1]);
|
|
|
|
|
|
return JSONObject.fromObject(db.query(sql)).toString();
|
|
|
}
|
|
|
jdbcTemplate = new JdbcTemplate();
|
|
|
jdbcTemplate.setDataSource(dasicDataSource);
|
|
|
}
|
|
|
System.out.println(jdbcTemplate.queryForMap("select 1 from dual"));
|
|
|
System.out.println(jdbcTemplate.queryForList(sql));
|
|
|
JSONArray jo = JSONArray.fromObject(jdbcTemplate.queryForList(sql));
|
|
|
XMLSerializer xmlSerializer = new XMLSerializer();
|
|
|
xmlSerializer.setRootName("req");
|
|
|
xmlSerializer.setElementName("data");
|
|
|
xmlSerializer.setTypeHintsEnabled(false);//设置data class="object" 没有class
|
|
|
// xmlSerializer.setForceTopLevelObject(false);
|
|
|
// xmlSerializer.setRemoveNamespacePrefixFromElements(false);
|
|
|
String xml = xmlSerializer.write(jo);
|
|
|
return xml;
|
|
|
} catch (Exception e1) {
|
|
|
e1.printStackTrace();
|
|
|
return "false";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|