OAF_JDBC系列2 - 通过JDBC连接SQLSERVER数据库DriverManager.getConnection

时间:2022-09-22 11:49:38

d

 

       try{
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          String connectionSQLServer = "jdbc:sqlserver://gavinmysql.com;databaseName=MDS;user=ViewMDSUserReadonly;password=MDSReadonly;";
          Statement stmtSource = null;
          Statement xstm = null;
          ResultSet rsSource = null;
          Connection msdbConn = DriverManager.getConnection(connectionSQLServer);
          try{
              userGroup[] users = new userGroup[90000];
              String msSql = null;
             
              if (xxAccountName !=null && !"".equals(xxAccountName))
              {
                msSql ="select ID,AdsPath,AccountName,AccountType,EmployeeNo,Name,DisplayName,Description, " +
                        "DepartmentID,DepartmentName,MemberOf,ObjectClass,Mail,TelephoneNumber,Mobile," +
                        "UserPrincipalName,SN,GivenName,Title,Department,Company,DirectReports,ManagerID," +
                        "Manager,OrderID,SyncTime,OrgID,OrgNumber,OrgNickNumber,OrgName,OrgDisplayName," +
                        "Disabled,Deleted from dbo.View_Mds_User where upper(AccountName)=" +"'"+ xxAccountName.toUpperCase() +
                        "' and Disabled="+"'"+"0"+"'";
              }else
              {
                    msSql ="select ID,AdsPath,AccountName,AccountType,EmployeeNo,Name,DisplayName,Description, " +
                            "DepartmentID,DepartmentName,MemberOf,ObjectClass,Mail,TelephoneNumber,Mobile," +
                            "UserPrincipalName,SN,GivenName,Title,Department,Company,DirectReports,ManagerID," +
                            "Manager,OrderID,SyncTime,OrgID,OrgNumber,OrgNickNumber,OrgName,OrgDisplayName," +
                            "Disabled,Deleted from dbo.View_Mds_User where Disabled="+"'"+"0"+"'";
              }
              
              stmtSource = msdbConn.createStatement();
              rsSource = stmtSource.executeQuery(msSql);
              int r =0;
              while (rsSource.next()){
                  users[r] = new userGroup();
                  users[r].strID = rsSource.getString("ID");
                  users[r].strAdsPath = rsSource.getString("AdsPath");
                  users[r].strAccountName = rsSource.getString("AccountName");
                  users[r].strAccountType = rsSource.getString("AccountType");
                  users[r].strEmployeeNo = rsSource.getString("EmployeeNo");
                  users[r].strName = rsSource.getString("Name");
                  users[r].strDisplayName = rsSource.getString("DisplayName");
                  users[r].strDescription = rsSource.getString("Description");
                  users[r].strDepartmentID = rsSource.getString("DepartmentID");
                  users[r].strDepartmentName = rsSource.getString("DepartmentName");
                  users[r].strMemberOf = rsSource.getString("MemberOf");
                  users[r].strObjectClass = rsSource.getString("ObjectClass");
                  users[r].strMail = rsSource.getString("Mail");
                  users[r].strTelephoneNumber = rsSource.getString("TelephoneNumber");
                  users[r].strMobile = rsSource.getString("Mobile");
                  users[r].strUserPrincipalName = rsSource.getString("UserPrincipalName");
                  users[r].strSN = rsSource.getString("SN");
                  users[r].strGivenName = rsSource.getString("GivenName");
                  users[r].strTitle = rsSource.getString("Title");
                  users[r].strDepartment = rsSource.getString("Department");
                  users[r].strCompany = rsSource.getString("Company");
                  users[r].strDirectReports = rsSource.getString("DirectReports");
                  users[r].strManagerID = rsSource.getString("ManagerID");
                  users[r].strManager = rsSource.getString("Manager");
                  users[r].strOrderID = rsSource.getString("OrderID");
                  users[r].strSyncTime = rsSource.getString("SyncTime");
                  users[r].strOrgID = rsSource.getString("OrgID");
                  users[r].strOrgNumber = rsSource.getString("OrgNumber");
                  users[r].strOrgNickNumber = rsSource.getString("OrgNickNumber");
                  users[r].strOrgName = rsSource.getString("OrgName");
                  users[r].strOrgDisplayName = rsSource.getString("OrgDisplayName");
                  users[r].strDisabled = rsSource.getString("Disabled");
                  users[r].strDeleted = rsSource.getString("Deleted");
                  //users[r].strChangeTime = rsSource.getString("ChangeTime");
                  r += 1;
              }