简体   繁体   中英

Coldfusion 9 DSN less Connection MSSQL 2008 Windows Authentication

Update:

Though the original question asked about windows authentication, any connection method would be fine. As long as it does not require creating a DSN.


I'm trying to connect my database with Windows Authentication. 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? 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". 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

Update:

If you absolutely must use a manual connection, try using the built in driver instead of the JDBCODBC bridge. 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. So overall this method will be less efficient than simply using a 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. See also Connecting with Integrated Authentication On Windows . However, that requires adding the driver jars and a DLL (sqljdbc_auth.dll) to the server, which it sounds like you cannot do.

  • Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver
  • URL: jdbc:sqlserver://localhost:1433;integratedSecurity=true;

Found an example here that shows the username and password in the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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