HI THIS TOUTORIAL WILL HELP FULL FOR STARTUP IN GRAILS CREATED BY NAVEENRAJU
Parts
Part8: Ternery RelationShip in sql by using Grails views
Part9:Gsp Tags And there Implimentation in gsp Pages
Part4: creating connection to mysql5.5 data base and opp
Saving data in to mysql and data base operations
1.By Default data is storing on h2 Database how
Step1: grails create-app MysqlDemo
Step2:grails create-domain-class Mysql
Step3:Code like this in folloing path
C:/MysqlDemo/grails-app/domain/mysqlDemo/Mysql.groovy
package mysqldemo
class Mysql {
String name
String age
String country
static constraints = {
}
}
Step4: grails create-controller Mysql
Step5:go to folloing path and code like this
C:/MysqlDemo/grails-app/controller/mysqlDemo/MysqlController.groovy
package mysqldemo
class MysqlController {
static scaffold=Mysql
}
Step6: Open following paths then u can see code like this
C:/MysqlDemo/grails-app/conf/DataSource.groovy
dataSource {
pooled = true
driverClassName =
"org.h2.Driver"
username =
"sa"
password =
""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate =
"create-drop" // one of 'create', 'create-drop', 'update',
'validate', ''
url =
"jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
test {
dataSource {
dbCreate =
"update"
url =
"jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
production {
dataSource {
dbCreate =
"update"
url =
"jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
pooled =
true
properties
{
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
Step6: grails run-app
Know browse
with that server generated ip address
u can
fill the form all the information is stored in
h2
database
Note: From above code
1)
pooled = true
driverClassName =
"org.h2.Driver"<---------------------- DriverClass
username =
"sa"<------------------------------------------------H2Database
UserName
password =
""<---------------------------------------------------H2Database
Password
2)
environments {
development {<--------------------------------For
Development Environment
dataSource
{<--------------------------------DateSourceConfiguring
dbCreate =
" create-drop " // one of 'create', 'create-drop', 'update',
'validate', ''
create-drop:
for every time u are
running ur application it will create db while server off it will Drop the
database tables
Update: If u want to fix the table in db permanently use
"Update" For every updating Of server it won't need to create db
tables just it will update rows and columns what u entered .Previous data will
be on database as usual
url =
"jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
jdbc:h2--------------------------------->In H2 DB
devDb
--------------------------------->DataBaseSchemaName
2)Same program i am going to store values in Mysl how
Step1:Open folloing path and recode like this
C:/MysqlDemo/grails-app/conf/DataSource.groovy
dataSource {
pooled = true
driverClassName =
"com.mysql.jdbc.Driver"//<-------------------Sql Deiver Class
username =
"root"//<------------------------Sql User Name
password =
"root"//<-------------------------Sql Password
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class =
'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate =
" create-drop" // one of 'create', 'create-drop', 'update',
'validate', ''
url =
"jdbc:mysql://localhost/MysqlDemo"
// jdbc:mysql:<------------------------------jdbc mysql
Driver
//Localhost<--------------------------------ServerRunning
Under
//MysqlDemo<-----------------------------DataBaseSchema
}
}
// for test and production is also same like devolopment
test {
dataSource {
dbCreate =
"update"
url =
"jdbc:mysql://localhost/ MysqlDemo "
}
}
production {
dataSource {
dbCreate =
"update"
url =
"jdbc:mysql://localhost/ MysqlDemo "
pooled =
true
properties
{
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
Step2:Open following path and remove comment line like this
C:/MysqlDemo/grails-app/conf/BuildConfig.groovy
// runtime
'mysql:mysql-connector-java:5.1.20' //<----------------Remove Comment On
Line 37
Then The code will be
grails.servlet.version = "2.5" // Change depending
on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir =
"target/test-classes"
grails.project.test.reports.dir =
"target/test-reports"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file =
"target/${appName}-${appVersion}.war"
grails.project.dependency.resolution = {
// inherit Grails'
default dependencies
inherits("global") {
// specify
dependency exclusions here; for example, uncomment this to disable ehcache:
// excludes
'ehcache'
}
log
"error" // log level of Ivy resolver, either 'error', 'warn', 'info',
'debug' or 'verbose'
checksums true //
Whether to verify checksums on resolve
repositories {
inherits true
// Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
grailsCentral()
mavenLocal()
mavenCentral()
// uncomment
these (or add new ones) to enable remote dependency resolution from public
Maven repositories
//mavenRepo
"http://snapshots.repository.codehaus.org"
//mavenRepo
"http://repository.codehaus.org"
//mavenRepo
"http://download.java.net/maven/2/"
//mavenRepo
"http://repository.jboss.com/maven2/"
}
dependencies {
// specify
dependencies here under either 'build', 'compile', 'runtime', 'test' or
'provided' scopes eg.
runtime 'mysql:mysql-connector-java:5.1.20'
}
plugins {
runtime
":hibernate:$grailsVersion"
runtime
":jquery:1.7.2"
runtime
":resources:1.1.6"
// Uncomment
these (or add new ones) to enable additional resources capabilities
//runtime
":zipped-resources:1.0"
//runtime
":cached-resources:1.0"
//runtime
":yui-minify-resources:0.1.4"
build
":tomcat:$grailsVersion"
runtime
":database-migration:1.1"
compile
':cache:1.0.0'
}
}
Step3:Open MysqlDataBase
mysql> Create schema MysqlDemo
-> ;
Query OK, 1 row affected (0.00 sec)
mysql>
use MysqlDemo;
Database
changed
mysql>
show tables;
Empty
set (0.00 sec)
Step4: grails run-app
Step5: enter the data like this
Step6: know in data base u can see output like this
Step7:Off the Server
Step8: in data base all tables are droped
Note to avoid this in DataSource.groovy
in devolopment Remove "create-drop" and rename as
"Update"
3)How to get value from mysql table to our Grails Controlleri am doing modifications to above controller
Step1: go to following path and code like this
C:/MysqlDemo/grails-app/controller/mysqlDemo/MysqlController.groovy
package mysqldemo
import groovy.sql.Sql
class MysqlController {
def scaffold=Mysql
def dataSource
def retrive={
def db = new Sql(dataSource)
def results12=db.rows("select DISTINCT mysql.id,mysql.age,mysql.name,mysql.country
from mysql ")
[results12:results12]
}}
Step2: create a view in folloing path and code like this
C:/MysqlDemo/grails-app/Views/mysql/retrive.gsp
<!DOCTYPE html>
<html>
<head>
<meta name="layout"
content="main">
</head>
<body>
<table>
<g:each
var="s" in="${results12}">
<tr>
<td>
<h1> ${s.id}</h1>
</td><td>
<h1> ${s.name}</h1></td><td>
<h1> ${s.age}</h1></td><td>
<h1>
${s.country}</h1></td></tr>
</g:each>
</table>
</body>
</html>
Step:3 now run this application
grails run-app
and enter the data in database data is stored like this in mysql
Step4:If u whant to retrive data from db then type like this in address bar
http://localhost:8080/MysqlDemo/mysql/retrive
from db it will retrieves data like this