How to create a DSNless database connection

The following article explains how to create a DSNless connection to a database. DSNless connections are direct connections to a database that do not require a DSN configured within the ColdFusion Administrator. A DSNless connection can be used to connect to any type of database, including Microsoft SQL; MySQL and Microsoft Access.

To create a DSNless connection, add the following snippet of code within your application.cfm or application.cfc page. Alternatively you can add this code to each individual page that needs to connect to a database:

<cfscript>

classLoader = createObject("java", "java.lang.Class");
classLoader.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dm = createObject("java","java.sql.DriverManager");

con = dm.getConnection(YOUR connectionstring GOES HERE);

st = con.createStatement();
rs = st.ExecuteQuery("Select * FROM table");
q = createObject("java", "coldfusion.sql.QueryTable").init(rs); >

</cfscript>


The connectionstring always depends on the type of database used. Following are a few common connectionstring examples:

Microsoft SQL

"jdbc:odbc:DRIVER={SQL Server};Database="yourdatabase";Server=localhost;", "yourusername", "yourpassword"

MySQL

"jdbc:odbc:DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306; DATABASE=yourdatabase; USER=yourusername; PASSWORD=yourpassword; OPTION=3;"

Microsoft Access (We recommend the \data\ folder located above your wwwroot)

"Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\HostingSpaces\yourdom\YourDomain.com\data\yourdatabase.mdb;Uid=;Pwd=;"


Please Note: Any connectionstring needs to be configured with the proper connection information.

DSNless connections are preferred and take less time to start using. If you require a DSN configured within the ColdFusion Administrator; 
be sure to create your Database connection for the DSN; then simply open a support ticket request that contains the proper connection information.

This article pertains to ColdFusion 8 and ColdFusion 9


Was this article helpful?

mood_bad Dislike 1
mood Like 0
visibility Views: 3835