简体   繁体   English

Coldfusion 9 DSN less连接MSSQL 2008 Windows身份验证

[英]Coldfusion 9 DSN less Connection MSSQL 2008 Windows Authentication

Update: 更新:

Though the original question asked about windows authentication, any connection method would be fine. 虽然原始问题询问了Windows身份验证,但任何连接方法都没问题。 As long as it does not require creating a DSN. 只要它不需要创建DSN。


I'm trying to connect my database with Windows Authentication. 我正在尝试使用Windows身份验证连接我的数据库。 Here is the code. 这是代码。 Please advise. 请指教。 Thanks. 谢谢。

<cfscript>
  classLoader = createObject("java", "java.lang.Class");
  classLoader.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
  dm = createObject("java","java.sql.DriverManager");
  dburl = "jdbc:sqlserver://192.168.3.150:1433;databasename=RsDB;integratedSecurity=true";
  con = dm.getConnection(dburl,"", "");
  st = con.createStatement();
  rs = st.ExecuteQuery("SELECT top 10 * FROM pa (nolock)");
  q = createObject("java", "coldfusion.sql.QueryTable").init(rs);   
</cfscript>

<cfoutput query="q">
  email = #email#<br />
</cfoutput>

ERROR : 错误:

Error Occurred While Processing Request This driver is not configured for integrated authentication. 处理请求时发生错误此驱动程序未配置为集成身份验证。

(Ignoring your question for a moment, ... ) (暂时忽略你的问题,......)

While it is possible to do it with a manual connection, is there a specific reason you cannot just use a standard DSN? 虽然可以通过手动连接来实现,但是有一个特定的原因你不能只使用标准的DSN吗? That would be far more efficient and require a lot less code/cleanup. 这样效率会更高,并且需要更少的代码/清理。 Not to mention that it avoids some of the common pitfalls of manual connections . 更不用说它避免了手动连接的一些常见缺陷

AFAIK, you can set the DSN to use windows authentication , by entering AuthenticationMethod=Type2 under "Show Advanced Settings > Connection String". AFAIK,您可以通过在“显示高级设置>连接字符串”下输入AuthenticationMethod=Type2来将DSN设置为使用Windows身份 AuthenticationMethod=Type2 Leave the "username" and "password" fields blank. 将“用户名”和“密码”字段留空。

<cfquery name="qTest" datasource="YourDSN">
   SELECT SYSTEM_USER AS LoginName, DB_NAME() AS DatabaseName
</cfquery>

<cfdump var="#qTest#" label="Show Connection Settings">

*NB: The DSN will use the CF Service user account, so adjust as needed *注意: DSN将使用CF服务用户帐户,因此请根据需要进行调整

Update: 更新:

If you absolutely must use a manual connection, try using the built in driver instead of the JDBCODBC bridge. 如果绝对必须使用手动连接,请尝试使用内置驱动程序而不是JDBCODBC桥。 However, be sure to always close the database objects. 但是,请务必始终关闭数据库对象。 Otherwise, open connections will start to pile up and bad things will happen. 否则,打开的连接将开始堆积,并且会发生不好的事情。

Keep in mind, opening database connections is expensive. 请记住,打开数据库连接非常昂贵。 By not using a DSN, you lose out on the benefits of connection pooling. 如果不使用DSN,您将失去连接池的优势。 So overall this method will be less efficient than simply using a DSN. 总的来说,这种方法的效率低于简单使用DSN的效率。

<cfscript>
   classLoader = createObject("java", "java.lang.Class");
   classLoader.forName("macromedia.jdbc.MacromediaDriver"); 
   dm = createObject("java","java.sql.DriverManager");
   dburl = "jdbc:macromedia:sqlserver://127.0.0.1:1433;databasename=RsDB;AuthenticationMethod=Type2";
   con = dm.getConnection(dburl);
   st = con.createStatement();
   rs = st.ExecuteQuery("SELECT getDate() AS TestValue");
   q = createObject("java", "coldfusion.sql.QueryTable").init(rs); 
   // ALWAYS close all objects!
   rs.close();
   st.close();
   con.close();
   writeDump(q);
</cfscript>

You could also use the SQL Server JDBC Driver instead. 您也可以使用SQL Server JDBC驱动程序 See also Connecting with Integrated Authentication On Windows . 另请参阅在Windows上连接集成身份验证 However, that requires adding the driver jars and a DLL (sqljdbc_auth.dll) to the server, which it sounds like you cannot do. 但是,这需要将驱动程序jar和DLL(sqljdbc_auth.dll)添加到服务器,这听起来是你无法做到的。

  • Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver 驱动程序类: com.microsoft.sqlserver.jdbc.SQLServerDriver
  • URL: jdbc:sqlserver://localhost:1433;integratedSecurity=true; URL: jdbc:sqlserver://localhost:1433;integratedSecurity=true;

Found an example here that shows the username and password in the URL. 在此处找到一个示例,显示URL中的用户名和密码。

<cfset connection = driverManager
.getConnection("jdbc:mysql://localhost:3306/demodb?user=demouser&password=demopass")>

Your URL for SQL Server doesn't pass in any credentials, give that a try. 您的SQL Server URL未传递任何凭据,请尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM