Can I get steps to connect my grails to MSSQL? - sql-server-2008

Please help me with steps to connect grails to SQLserver2008.
I have been trying with setting over grails app-config.properties and DataSource.groovy file too.
File 1 : app-config.properties
dataSource.dialect =org.hibernate.dialect.SQLServerDialect
dataSource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
dataSource.url=jdbc:sqlserver:thin:#xxx.xxx.x.:1433;databaseName=xxxx
dataSource.username=sa
dataSource.password=P#ssw0rd1x
File 2 : Config.groovy
activiti {
processEngineName = "activiti-engine-default"
databaseType = "sql"
databaseSchemaUpdate = true
deploymentName = appName

Not sure if it will work with the mssql-2008, but for my application connection with mssql 2012 I have used following changes to connect it with the database:
Step1: On the datasource.groovy changed the datasource to:
dataSource {
pooled = true
driverClassName = "net.sourceforge.jtds.jdbc.Driver"
url = "jdbc:jtds:sqlserver://localhost:1433;DatabaseName=<Db-Name>"
username = "sa"
password = "root"
dbCreate = "update" // one of 'create', 'create-drop','update'
logSql = true
}
Step2. Then I have placed jtds-1.2.5.jar and jtidy-r938.jar files in the lib folder.
And I think it should connect with the mssql db, for me it worked with sql server 2012, hope it should also work with 2008.
I think need not to make any other change anywhere else. :)

Related

How to connect to mysql or postgresql database running on a cloud server with appscripts?

I am trying to connect to a postgresql server, which is currently running on a google cloud machine with appscripts. I want to push the data from the google spreadsheet to that postgresql database, I have tried to achieve this with the help of jdbc documentation provided in this documentation. I have gone through the documentation but couldn't find anything related to postgresql, so i tried connecting with mysql database running on the same server but it is showing the error
The code which I tried for connecting to mysql looks like
var server = 'ipaddress'
var dbName = 'dbname'
var username = 'db name'
var password = 'password'
var port = 3306
var url = 'jdbc:mysql://' + server + ":" + port + "/" + dbName;
function myFunction() {
var conn = Jdbc.getConnection(url, username, password)
Logger.log(url);
Logger.log(conn);
conn.close();
}
My current code for postgresql looks like
var dbName = 'dbname'
var username = 'username'
var password = 'password'
var port = 5432
function myFunction() {
var url = 'jdbc:postgresql://' + server + ":" + port + "/" + dbName;
var conn = Jdbc.getConnection(url, username, password)
Logger.log(url);
Logger.log(conn);
conn.close();
}
I am getting the error
How to solve this? and is it even possible to connect to postgresql with appscripts?
PS I am able to connect to the same database with same credentials with pgadmin
From Apps Script documentation here :
The JDBC service supports Google Cloud SQL MySQL, MySQL, Microsoft SQL
Server, and Oracle databases.
So PostgreSQL is not yet supported.
For the MySQL connection, from the error message, it is clear that your credentials seems wrong. Check it by login to your database remote server via terminal
mysql -u <username> -p -h <ip_address>
or connect it via Database Tools like DBeaver

Node js mysql wrapper error - Object #<Object> has no method 'table'

I am trying a nodejs mysql wrapper which I got from here.Everything is working including the setup and connection. But when I try saving a piece of data into the table, I get the error Object #<Object> has no method 'table'. When I just console the db object,I get the object, no error.
Can someone tell what I miss here ?
my global.js
//connect to mysql
var mysql = require('mysql');
var connection = mysql.createConnection({host : 'xxx.xxx.xxx.xxx', user : 'xxxx', password : 'xxxx', database : 'xxxx' });
connection.connect();
var wrapper = require('node-mysql-wrapper');
var db = wrapper.wrap(connection);
exports.db = db;
my app.js
var db = require('./global.js');
db.table('my_table').save( my_data_object );
The problem is that you're exporting a separate property instead of exporting directly. So either change exports.db = db to module.exports = db or change var db = require('./global.js') to var db = require('./global.js').db.
Additionally, you are referencing global.js instead of config.js as you show in your question, so depending on whether or not that is a typo, that could be another issue.

Grails run query postgresql on second database to generate Raw JSON

I have one postgreSQL database which I am using inside my Grails application(configured in Datasource.groovy), lets call it DB1. Now I have other postgreSQL database which has lots of data inside it, lets call it DB2.
I am writing an data export procedure which takes in JSON data generated from DB2, make its domain objects and store it inside the DB1. This data is being sent from another software using DB2. The main problem is that both databases have different column names so it cannot be a direct import export.
PostgreSQL provides direct methods to generate JSON via SQL queries. Eg-
SELECT row_to_json(t)
FROM ( select id, descrizione as description from tableXYZ ) t
It returns a JSON output-
{"id":6013,"description":"TestABC"}
This JSON can be consumed by the code that I have made.
So I want to run this query on DB2 from inside grails application which has DB1 configured inside Datasource.groovy.
How to do that?
In your DataSource.groovy file, you need to create another data source to point at DB2. You can probably clone your dataSource definition for this, eg.
http://grails.github.io/grails-doc/2.3.9/guide/conf.html#multipleDatasources
dataSource_db2 {
pooled = true
jmxExport = true
driverClassName = "org.postgresql.Driver"
username = "XXX"
password = "YYY"
//noinspection GrReassignedInClosureLocalVar
dialect = PostgreSQLDialect
autoreconnect = true
useUnicode = true
characterEncoding = "utf-8"
tcpKeepAlive = true
//noinspection GroovyAssignabilityCheck
properties {
// See http://grails.org/doc/latest/guide/conf.html#dataSource for documentation
initialSize = 5
maxActive = 50
minIdle = 5
maxIdle = 25
maxWait = 10000
maxAge = 10 * 60000
timeBetweenEvictionRunsMillis = 1000 * 60 * 1 // 1 min
minEvictableIdleTimeMillis = 1000 * 60 * 5 // 5 min
numTestsPerEvictionRun = 3
validationQuery = 'SELECT 1'
validationQueryTimeout = 3
validationInterval = 15000
testOnBorrow = true
testWhileIdle = false
testOnReturn = false
defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED
removeAbandoned = true
removeAbandonedTimeout = 20 // 20s is a long query
logAbandoned = true // causes stacktrace recording overhead, use only for debugging
// use JMX console to change this setting at runtime
// the next options are jdbc-pool specific
// http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#Common_Attributes
// https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/jdbc/pool/PoolConfiguration.html
jmxEnabled = true
// ResetAbandonedTimer resets the timer upon every operation on the connection or a statement.
// ConnectionState caches the auto commit, read only and catalog settings to avoid round trips to the DB.
jdbcInterceptors = "ConnectionState;ResetAbandonedTimer;SlowQueryReportJmx(threshold=10000)"
abandonWhenPercentageFull = 25 // settings are active only when pool is full
}
}
To use it for database connection access, you can inject the javax.sql.DataSource into your Services, Controllers, Domain classes, or other Grails Artefakts.
eg.
import javax.sql.DataSource
import groovy.sql.GroovyResultSet
import groovy.sql.Sql
MyService {
DataSource dataSource_db2
def doQuery(String query) {
new Sql(dataSource_db2).rows(query)
}
}
To have a domain object use your db2 dataSource for GORM, add to your domain objects 'mapping' block:
static mapping = {
datasource 'db2'
}
If you want JNDI support, you can also add something like this in your resources.groovy:
xmlns jee: "http://www.springframework.org/schema/jee"
jee.'jndi-lookup'(id: "dataSource", 'jndi-name': "java:comp/env/jdbc/db1")
jee.'jndi-lookup'(id: "dataSource_db2", 'jndi-name': "java:comp/env/jdbc/db2")

How to use MySQL and MSSQL together in the grails datasource?

I have an grails application that uses MySQL for authentication purpose and another application that uses MSSQL for database stuff. I need to combine these together as one application. The datasource for MySQL contains the following
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
The datasource for application using MSSQL contains the following
dataSource {
pooled = true
driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver" //jdbc driver downloaded from internet: sqljdbc4.jar and sqljdbc_auth.dll (see DisplayHistorical/grails-app/lib)
dialect = "org.hibernate.dialect.SQLServer2008Dialect"
ClassName = "org.hsqldb.jdbcDriver" //Original Code
// enable loggingSql to see sql statements in stdout
loggingSql = true
}
How would I combine these? I looked at the tutorial mentioned on this site (How do you access two databases in Grails) but it doesnt talk about adding the drivers
If you follow the link provided earlier, then you would end up with a datasource configuration like below:
environments {
production {
dataSource_authentication {
pooled = true
url = "jdbc:mysql://yourServer/yourDB"
driverClassName = "com.mysql.jdbc.Driver"
username = "yourUser"
password = "yourPassword"
........
}
dataSource {
pooled = true
driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
dialect = "org.hibernate.dialect.SQLServer2008Dialect"
........
}
}
}
Where ever required you can use the authentication datasource explicitly.

Codeigniter remote connection does not return queries

I have a local and a remote connection with my mysql database. The local connection works just fine. But the remote connection, while it makes a connection, it does not return anything. I usually get the following:
Fatal error: Call to a member function result() on a non-object
I use for the remote connection the following configuration:
$db['mydb']['hostname'] = "ip_address_of_database";
$db['mydb']['username'] = "username";
$db['mydb']['password'] = "password";
$db['mydb']['database'] = "database";
$db['mydb']['dbdriver'] = "mysql";
$db['mydb']['dbprefix'] = "";
$db['mydb']['pconnect'] = FALSE;
$db['mydb']['db_debug'] = FALSE;
$db['mydb']['cache_on'] = FALSE;
$db['mydb']['cachedir'] = "";
$db['mydb']['char_set'] = "utf8";
$db['mydb']['dbcollat'] = "utf8_general_ci";
In my function that accesses the database I check if there is a connection with the remote server and then I try to retrieve data.
$mydb = $this->load->database('mydb', TRUE);
if (!isset($mydb->conn_id) && !is_resource($mydb->conn_id)) {
$error = 'database is not connected';
return $error;
}else{
$query = $mydb->query("SELECT * FROM database LIMIT 1;");
return $query->result();
}
This works fine in the localhost database but not in the remote database. I allways get the error
Fatal error: Call to a member function result() on a non-object
Can you please help? What am I doing wrong? I stuck on this.
Finally, I found the solution after contacting my web hosting provider. The issue had to do with the Remote database access and their servers. The IP address exception and the domain name that I had added didn’t do the job. I had to add an internal domain name that my host was using in order the Remote database access to be allowed. I spent 2-3 hours chatting with them in order to find a solution.
Anyway now is solved. I am posting that FYI.