dataSourceJs.jsp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
  2. <%@include file="/WEB-INF/commons/commonInclude.jsp" %>
  3. <script>
  4. var paramManager = {
  5. //初始化
  6. init:function(){
  7. var me = this;
  8. $("#txtType").ligerComboBox({data:[{code:'mysql',value:'MySQL'},{code:'oracle',value:'Oracle'},{code:'sqlserver',value:'SQL Server'}]});
  9. $("#divForm").ligerAutoForm({
  10. data:me.getModel(),
  11. validate:{
  12. name:"required",
  13. type:"required",
  14. ip:"required",
  15. port:"required",
  16. dbName:"required",
  17. dbUser:"required",
  18. dbPassword:"required"
  19. },
  20. message:{
  21. name:"请输入数据源名称",
  22. type:"请选择数据库类型",
  23. ip:"请输入主机名或IP地址",
  24. port:"请输入端口",
  25. dbName:"请输入数据库名",
  26. dbUser:"请输入用户名",
  27. dbPassword:"请输入密码"
  28. }
  29. });
  30. me.initEven();
  31. },
  32. //绑定按钮事件
  33. initEven:function(){
  34. var me = this;
  35. $("#btnTest").click(function(){
  36. me.test();
  37. });
  38. $("#btnSave").click(function(){
  39. me.save();
  40. });
  41. $("#btnCancel").click(function(){
  42. var model = me.getModel();
  43. $("#divForm").ligerAutoForm("setData",model);
  44. });
  45. },
  46. //生成数据库连接字符串
  47. getConfigString:function(data){
  48. var resultStr="";
  49. if(data.type=="mysql"){//mysql
  50. resultStr ="jdbc:mysql://"+ data.ip +":"+ data.port +"/"+ data.dbName +"?user="+ data.dbUser +"&password="+ data.dbPassword +"&useUnicode=true&characterEncoding=UTF-8";
  51. }
  52. else if(data.type=="oracle"){//oracle
  53. resultStr ="jdbc:oracle:thin:"+ data.dbUser +"/"+ data.dbPassword +"@//"+ data.ip +":"+ data.port +"/"+ data.dbName ;
  54. }
  55. else{//sqlserver
  56. resultStr ="jdbc:sqlserver://"+data.ip+":"+data.port+"/"+data.dbName+"?user="+data.dbUser+"&password="+data.dbPassword;
  57. }
  58. return resultStr;
  59. },
  60. //获取初始值
  61. getModel:function(){
  62. var model;
  63. if('${model.id}'.length>0) {
  64. var configStr = '${model.config}';
  65. model = {
  66. id : '${model.id}',
  67. name : '${model.name}'
  68. };
  69. //反解析连接字符串
  70. if(configStr!=null && configStr.length>0)
  71. {
  72. var type = (configStr.split("\/")[0]).split(":")[1];
  73. model.type = type;
  74. if( type == "mysql" || type == "sqlserver"){
  75. model.ip = (configStr.split("\/")[2]).split(":")[0];
  76. model.port = (configStr.split("\/")[2]).split(":")[1];
  77. model.dbName = (configStr.split("\/")[3]).split("?")[0];
  78. model.dbUser = (configStr.split("\/")[3]).split("?")[1].split("&")[0].split("=")[1];
  79. model.dbPassword = (configStr.split("\/")[3]).split("?")[1].split("&")[1].split("=")[1];
  80. }else{
  81. model.ip = (configStr.split("\/")[3]).split(":")[0];
  82. model.port = (configStr.split("\/")[3]).split(":")[1];
  83. model.dbName = configStr.split("\/")[4];
  84. model.dbUser = (configStr.split("\/")[0]).split(":")[3];
  85. model.dbPassword = (configStr.split("\/")[1]).split("@")[0];
  86. }
  87. }
  88. }
  89. else{
  90. $('#dataSourceInfo').show();
  91. }
  92. return model;
  93. },
  94. //测试数据库连接
  95. test:function(){
  96. var me = this;
  97. if(!$("#divForm").ligerAutoForm("validate"))
  98. {
  99. return;
  100. }
  101. var data = $("#divForm").ligerAutoForm("getData");
  102. $.ajax({
  103. type: "POST",
  104. url : "${contextRoot}/system/testDataSource",
  105. dataType : "json",
  106. data:{uri:me.getConfigString(data)},
  107. cache:false,
  108. success :function(data){
  109. if(data.successFlg) {
  110. $.ligerDialog.success(data.message);
  111. }
  112. else{
  113. $.ligerDialog.error(data.message);
  114. }
  115. }
  116. });
  117. },
  118. //保存操作
  119. save:function(){
  120. var me = this;
  121. if(!$("#divForm").ligerAutoForm("validate"))
  122. {
  123. return;
  124. }
  125. var data = $("#divForm").ligerAutoForm("getData");
  126. $.ajax({
  127. type: "POST",
  128. url : "${contextRoot}/system/saveDataSource",
  129. dataType : "json",
  130. data:{id:data.id,name:data.name,config:me.getConfigString(data)},
  131. cache:false,
  132. success :function(data){
  133. if(data.successFlg) {
  134. $.ligerDialog.success(data.message);
  135. indexPage.refresh();
  136. }
  137. else{
  138. $.ligerDialog.error(data.message);
  139. }
  140. }
  141. });
  142. }
  143. };
  144. $(function(){
  145. paramManager.init();
  146. });
  147. </script>