How do I create a DSNless connection to a database?
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 ColdFusion Administrator to make specific configurations. A DSNless connection can be used to connect to any type of database, including Microsoft SQL and Microsoft Access.
To create a DSNless connection, add the following snippet of code within your application.cfm page or to each individual page that should 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(connection string);
st = con.createStatement();
rs = st.ExecuteQuery("Select * FROM table");
q = createObject("java", "coldfusion.sql.QueryTable").init(rs);
</cfscript>
The connectionstring will depend on the type of database. The following are two common connectionstring examples:
Microsoft SQL
"jdbc:odbc:DRIVER={SQL Server};Database="dbname";Server=db_servername.safesecureweb.com;",
"login", "password"
Microsoft Access
"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\websites\folder\mydatabase.mdb;Uid=;Pwd=;"
Note: If you use either of the above examples, you will need to change the information to refer to your database.