Creating a datasource in application.cfc

Is it possible to define a datasource in application.cfc WITHOUT using the server/web admin pages?

I’ve tried this with no luck. Here is my test.cfm.

<CFQUERY NAME="TestQry" DATASOURCE="mydb">
SELECT * FROM MyTable
</CFQUERY>

Here is my application.cfc

<CFCOMPONENT>
this.datasources["mydb"] = 
{ 
class: 'net.sourceforge.jtds.jdbc.Driver' , 
connectionString: 'jdbc:jtds:sqlserver://mysqldb:1433/mdb' , 
username: 'dbuser' , 
password: "dbpwd",
};
</CFCOMPONENT>

Error that appears on the page is:

Message datasource [mydb] doesn't exist 
Detail available datasource names are []

Yes, absolutely. The datasource set up in the admin should also give you the exact code to use in your Application.cfc for configuration.

Have you got a file called application.cfc or Application.cfc?

It’s Application.cfc.

After fiddling with things a little more, I changed Application.cfc based on the CF documentation (the original sample was taken from Lucee docs). Now it looks like this and it works.

this.datasources = 
{ 
	mydb= 
	{ 
		database = "mydb", 
		host = "mysqldb", 
        port = "1433", 
		class: 'net.sourceforge.jtds.jdbc.Driver' , 
		connectionString: 'jdbc:jtds:sqlserver://mysqldb:1433/mydb' , 
        username = "dbuser", 
        password = "dbpwd"
	},
};
1 Like

Awesome :tada:

A tip I always find useful… once you have created a datasource in the admin it will show the code required to configure it via Application.cfc at the bottom of the edit view:

The admin offers this type of advice in many locations.

2 Likes