我正在使用Weblogic和Microsoft SQL Server,我希望使用Java代码获取连接的用户名。代码返回“dbo”而不是“sa”,即登录名。
当我输入以下代码时:
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup(jndiName);
Connection connection = ds.getConnection();
connection.setAutoCommit(false);
System.out.println(conn.getMetaData().getUserName());
我尝试了两种选择: 首先我尝试了查询:
SELECT a.name AS Login
FROM sys.server_principals AS a
JOIN sys.database_principals as U
ON a.SID = U.SID
WHERE u.name = 'dbo';
正确返回“sa”但不幸的是我无法在我的代码中使用此查询,因为我需要的用户没有管理员权限,我无法访问这些表。
我尝试的第二个解决方案是以下代码:
Method getter = new PropertyDescriptor("user", ds.getClass()).getReadMethod();
String value = (String)getter.invoke(ds);
但这对我来说也不起作用,因为我得到以下例外:
java.beans.IntrospectionException: Method not found: isUser
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:106)
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.thortech.xl.dataobj.rulegenerators.adpPBCASHIERCREATEDBY.CREATEDBY(adpPBCASHIERCREATEDBY.java:105)
at com.thortech.xl.dataobj.rulegenerators.adpPBCASHIERCREATEDBY.implementation(adpPBCASHIERCREATEDBY.java:52)
at com.thortech.xl.dataobj.rulegenerators.tcBaseRuleGenerator.run(tcBaseRuleGenerator.java:176)
at com.thortech.xl.dataobj.rulegenerators.tcAdapterExecuter.executeRuleGenerators(tcAdapterExecuter.java:266)
at com.thortech.xl.dataobj.util.tcPrePopulateUtility.setDataFromAdapter(tcPrePopulateUtility.java:1094)
at com.thortech.xl.dataobj.util.tcPrePopulateUtility.prePopulate(tcPrePopulateUtility.java:389)
at com.thortech.xl.dataobj.util.tcOrderPackages.computeProcessFormData(tcOrderPackages.java:865)
at com.thortech.xl.dataobj.util.tcOrderPackages.createOrder(tcOrderPackages.java:428)
at com.thortech.xl.dataobj.util.tcOrderPackages.orderPackageForUser(tcOrderPackages.java:180)
at com.thortech.xl.dataobj.tcOIU.provision(tcOIU.java:869)
at com.thortech.xl.dataobj.tcOIU.eventPostInsert(tcOIU.java:364)
有没有人设法找到解决方案?如何通过数据源检索用户登录?
由于