Connect and using the database

Hi everybody,

I have a database name as “staff” and i configure "coldbox.cfc" as follow:

datasource = {
tinishop = { name = “staff”, dbType=“MSSQL”, username=“root”, password=“123”}

};

How do i test the connection and using it?

Best regards,

That configuration does NOT create a ColdFusion data source. You need to do that still in your administrator. There is a verify button there in the admin to test actual DB server connectivity.

The ColdBox setting just keeps track of the data source information for you so you don’t have to hard-code it into your cfquery or cfstoredproc tags. To test your ColdBox config, just write a cfquery that uses the ColdBox settings. Also, you didn’t say what version of ColdBox you’re on (please see the subject line conventions)

See this example code here:
http://wiki.coldbox.org/wiki/Compatibility:4.0.0.cfm#Datasource_Bean_Dropped

Inject the settings into your handlers and models using the ColdBox namespace in the WireBox injection DSL:
http://wiki.coldbox.org/wiki/WireBox.cfm#ColdBox_Namespace

property name=“tinishop” inject=“coldbox:datasource:tinishop”;

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Hi,

I have configured the data source in Railo server. I am using the Coldbox version 4.0.0. Please see this code below:

path: handlers\test.cfc

`
component {

property name=“abc” inject=“coldbox:datasource:tinishop”;
}
`

path: test\index.cfm

`


select *
from product

`

And notice’s web browser

Type: Builder.DSLDependencyNotFoundException Messages: The DSL Definition {REF={null}, REQUIRED={true}, ARGNAME={}, DSL={coldbox:datasource:tinishop}, JAVACAST={null}, NAME={abc}, TYPE={any}, VALUE={null}, SCOPE={variables}} did not produce any resulting dependency The target requesting the dependency is: 'handlers.test'
What’s wrong? Plz. give the feedback
Thank you so much,

Did you reinit the application?

Also, why is your query in the view? Put business logic in your handler, or better yet, a service. The error was related to the injection into the handler, but you’re also not actually passing ‘abc’ to the view. If you want to pass data from your handler to your view, place it in the “prc” (private request collection) struct inside the action method.

That said, if you’re doing the query in the view, just retrieve the data source information there.

<cfset abc = getInstance( dsl=“coldbox:datasource:tinishop” )>

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Now, I have setup the test.cfc file as below but it not active.
`

component {

property name=“abc” inject=“coldbox:datasource:tinishop”;

public function init(){
return this;
}

public any function default(event, rc, prc) {
prc.product = QueryExecute("SELECT * FROM product);
writeDump(prc.product);

}
}
`

URL: http://tinishop:8888/index.cfm/test

And notice’s web browser

`
Type: Builder.DSLDependencyNotFoundException
Messages: The DSL Definition {REF={null}, REQUIRED={true}, ARGNAME={}, DSL={coldbox:datasource:tinishop}, JAVACAST={null}, NAME={abc}, TYPE={any}, VALUE={null}, SCOPE={variables}} did not produce any resulting dependency The target requesting the dependency is: ‘handlers.test’

`

What’s wrong? Can you give me the examples code about connect the database?

Thanks so much,

Can you show your config. I just realized your initial message shows
datasource = {}
instead of
datasources = {}

Please make sure you’re checking the docs for the right syntax:
http://wiki.coldbox.org/wiki/ConfigurationCFC.cfm#datasources

Also, simply injecting the data source information into the CFC doesn’t actually do anything. You actually have to USE it in your query. Note, your queryExecute was missing an ending double quote in your code sample too.

QueryExecute(
“SELECT * FROM product”,
[],
{
datasource=abc.name,
dbtype=abc.dbtype,
username=abc.username,
password=abc.password
}
);

There’s nothing magic about the data source. It’s just a struct of data that you use for the actual query. The benifit of having it in a setting is the config is only in one place and can be changed easily, or switched on a per-environment basis. Note, pretty much all the data source settings are optional. You only need to specify them if you’re overriding the defaults.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

On a side note, if you plan on only using the variables in your handler, then don’t store them in the RC or PRC scope. Only store them in the PRC if you intend to use in viewlets or pass them as arguments for models/services as best practice.

I have configured the coldbox.cfc as below:

`
component {

// Configure ColdBox Application
function configure(){

// coldbox directives
coldbox = {
//Application Setup
appName = “Hoa Shop”,

//Development Settings
debugMode = true,
debugPassword = “”,
reinitPassword = “”,
handlersIndexAutoReload = true,
configAutoReload = false,
customErrorTemplate = ‘/coldbox/system/includes/BugReport.cfm’,

//Implicit Events
defaultEvent = “general.home”,

//Application Aspects
handlerCaching = false,
eventCaching = false
};

conventions = {
handlersLocation = “handlers”,
pluginsLocation = “plugins”,
viewsLocation = “views”,
layoutsLocation = “layouts”,
modelsLocation = “model”,
modulesLocation = “modules”,
eventAction = “index”
};
//thinh <- a database in MSSQL
datasource = {
tinishop = { name = “thinh”, dbType=“MSSQL”, username=“root”, password=“dev123”}
};

interceptors = [
{ class="#appMapping#.interceptors.LogBox" },
{class=“coldbox.system.interceptors.SES”,
properties = {configFile = “config/Routes.cfm”} }
];

}

}
`

Thanks,

So, based on the information in my last E-mail, can you spot the problem in your config?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

I have changedproperty name="abc"

to
property name="thinh"

I have not yet realized problem. Pls. help me!

Make sure you read what Brad said.

For your reference again.

Can you show your config. I just realized your initial message shows
datasource = {}
instead of
datasources = {}

Please make sure you’re checking the docs for the right syntax:
http://wiki.coldbox.org/wiki/ConfigurationCFC.cfm#datasources

Also, simply injecting the data source information into the CFC doesn’t actually do anything. You actually have to USE it in your query. Note, your queryExecute was missing an ending double quote in your code sample too.

QueryExecute(
“SELECT * FROM product”,
[],
{
datasource=abc.name,
dbtype=abc.dbtype,
username=abc.username,
password=abc.password
}
);

There’s nothing magic about the data source. It’s just a struct of data that you use for the actual query. The benifit of having it in a setting is the config is only in one place and can be changed easily, or switched on a per-environment basis. Note, pretty much all the data source settings are optional. You only need to specify them if you’re overriding the defaults.