SlideShare a Scribd company logo
Grails Tutorial
  Connecting to MySql
Part 1
Creating database in MySql
• By default, Grails applications are configured to
use an in-memory HSQLDB database for
development and testing
• Now we will create application to store student
information in database.
• Create 2 new databases that will be used by our
application
   • create database student_registration;
   • create database student_registration_prod;
Part 2
Create Application in Grails
• After creating the database, the next thing to do is
to create a new application.
   • grails create-app student-registraion
• After creating the application, the next step is to
setup the its dependencies. There are two ways of
setting up the application to use MySQL database,
However, I prefer this way since it is much easier.
•Inside the file BuildConfig.groovy file under grails-
app/conf, find and uncomment the line which says:
   • //runtime 'mysql:mysql-connector-java:5.1.16'
• As of this writing, the latest versioin of the JDBC is
5.1.18, so update accordingly.
• You also need to uncomment maven repository if it is
commented (mavenCentral())
• Note : if application failed to start, then add mysql-
connector-java-5.1.18-bin.jar in lib directory
• Now we are ready to add database details. We need
to Update DataSource.groovy file under grails-
app/conf.
   dataSource {
       pooled = true
       driverClassName = "com.mysql.jdbc.Driver"
      dialect =
   "org.hibernate.dialect.MySQL5InnoDBDialect"
   }
• We need to change the driverClassName to point to
the proper class.
• Also, since we are using the InnoDB engine, we will be
setting the dialect
   dialect =
   "org.hibernate.dialect.MySQL5InnoDBDialect"
• Note : If application failed to start bcoz of database
credentials add the 2 entries after dialect
   • username=“username”
   • password=“password”
• Next step just shows you how to configure
environment variable for database
• Let us use the two databases we created a while ago.
• For development & test environment, let us use the
student_registration database. On the other hand, for
the production environment, let us use the
student_registration_prod database.
For Development & Test use same datasource
development {
      dataSource {
                dbCreate = "create-drop"
              url =
"jdbc:mysql://localhost/student_registration?
useUnicode=yes&characterEncoding=UTF-8"
                     username = "root“
                password = ""
                }
      }
• Note for dbCreate read next slide
For Production use same datasource
production {
      dataSource {
               dbCreate = “update"
              url =
"jdbc:mysql://localhost/student_registration?
useUnicode=yes&characterEncoding=UTF-8"
                     username = "root“
               password = ""
               }
      }
dbCreate
•dbCreate deserves a special mention, It determines
what happens to your database between server
restarts.
• For example, a value of create-drop means that the
database tables will be dropped and then recreated
when you start the server, so you lose any data that
was in there. This is fine for testing, but is definitely
not what you want in production
Part 3
Create Domain class,Controller &
            Views
•The next step is to create a domain class that will be
persisted later in our database. Let’s create a sample
domain class.
   • create-domain-class com.reg.Student
• Now update the Student.groovy file in domain dir
   class Student {
       String studentID
       String firstName
       String middleName
       String lastName
       Integer year
       static constraints = {   }
   }
•The next step is to create a controller class
   • create-controller com.reg.Student
• Update the file StudentController.groovy under
grails-app/controller/com.reg/.
• Inside the controller, we will define a scaffold which
will automatically create the CRUD (Create, Read,
Update, Delete) functionality for our domain class


   class StudentController {
       def scaffold = Student
       def index() { }
   }
•The next step is to generate a view for Student
   • generate-views com.reg.Student
• There is no need to change in view (.gsp) pages. The
grails automatically creates all CRUD
(Create,Read,Update,Delete) functionality for you.
• You just need to care about the url mapping for all.
After running your application if your controller id
redirected to “index” then write “create” instead of
“index”.
• I don’t know where is url mapping configured in grails,
that’s why I use this to redirect the pages.
• Now its time to start your grails student-registration
application
   • grails run-app
• If your application starts without any errors, use the
url printed on console to access your app.
• Now connect to your student_registration database in
mysql
   • connect student_registration;
• Now you notice that new table student is created.
   • desc student;
• Now access the url and check all CRUD
(Create,Read,Update,Delete) functionality is working or
not.
   • http://localhost:8080/student-
   registraion/student/create
• Check the database values after each create,update &
delete.
Thank you
• I am not expert in grails, but if you have any doubts in
tutorial please feel free to contact me on
ashishkirpan@gmail.com

• Sorry for grammar mistakes :)

More Related Content

What's hot

Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
WebStackAcademy
 
Understanding Database Transactions and Hibernate Sessions in Grails
Understanding Database Transactions and Hibernate Sessions in GrailsUnderstanding Database Transactions and Hibernate Sessions in Grails
Understanding Database Transactions and Hibernate Sessions in Grails
Jonas Witt
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
iFour Technolab Pvt. Ltd.
 
Upload files with grails
Upload files with grailsUpload files with grails
Upload files with grails
Eric Berry
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
Eyal Vardi
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
Forziatech
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
NexThoughts Technologies
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Nascenia IT
 
jQuery
jQueryjQuery
Json Tutorial
Json TutorialJson Tutorial
Json Tutorial
Napendra Singh
 
Angular routing
Angular routingAngular routing
Angular routing
Sultan Ahmed
 
Expressjs
ExpressjsExpressjs
Avoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAvoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promises
Ankit Agarwal
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
Jadson Santos
 
Angular directives and pipes
Angular directives and pipesAngular directives and pipes
Angular directives and pipes
Knoldus Inc.
 
Javascript
JavascriptJavascript
Javascript
Nagarajan
 
Grails Controllers
Grails ControllersGrails Controllers
Grails Controllers
NexThoughts Technologies
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
NexThoughts Technologies
 

What's hot (20)

Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Understanding Database Transactions and Hibernate Sessions in Grails
Understanding Database Transactions and Hibernate Sessions in GrailsUnderstanding Database Transactions and Hibernate Sessions in Grails
Understanding Database Transactions and Hibernate Sessions in Grails
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Upload files with grails
Upload files with grailsUpload files with grails
Upload files with grails
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
jQuery
jQueryjQuery
jQuery
 
Json Tutorial
Json TutorialJson Tutorial
Json Tutorial
 
Angular routing
Angular routingAngular routing
Angular routing
 
Expressjs
ExpressjsExpressjs
Expressjs
 
Avoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAvoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promises
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 
Angular directives and pipes
Angular directives and pipesAngular directives and pipes
Angular directives and pipes
 
Javascript
JavascriptJavascript
Javascript
 
Grails Controllers
Grails ControllersGrails Controllers
Grails Controllers
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 

Similar to Grails Connecting to MySQL

Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Drupalcon Paris
 
Iac d.damyanov 4.pptx
Iac d.damyanov 4.pptxIac d.damyanov 4.pptx
Iac d.damyanov 4.pptx
Dimitar Damyanov
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
Hemo Chella
 
Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning service
Ruth Yakubu
 
Social Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes DemystifiedSocial Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes Demystified
Claudio Procida
 
Make Your Build Great Again (DroidConSF 2017)
Make Your Build Great Again (DroidConSF 2017)Make Your Build Great Again (DroidConSF 2017)
Make Your Build Great Again (DroidConSF 2017)
Jared Burrows
 
PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part IPROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part I
SivaSankari36
 
Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11
Jess Coburn
 
Spring boot
Spring bootSpring boot
Spring boot
Bhagwat Kumar
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
Engine Yard
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
michaelaaron25322
 
A to z for sql azure databases
A to z for sql azure databasesA to z for sql azure databases
A to z for sql azure databases
Antonios Chatzipavlis
 
Blue Green Sitecore Deployments on Azure
Blue Green Sitecore Deployments on AzureBlue Green Sitecore Deployments on Azure
Blue Green Sitecore Deployments on Azure
Rob Habraken
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC Applications
Sarvesh Kushwaha
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
Nuha Noor
 
Sa106 – practical solutions for connections administrators
Sa106 – practical solutions for connections administratorsSa106 – practical solutions for connections administrators
Sa106 – practical solutions for connections administrators
Sharon James
 
DrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizingDrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizing
Ashok Modi
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
Ana-Maria Mihalceanu
 

Similar to Grails Connecting to MySQL (20)

Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
 
Iac d.damyanov 4.pptx
Iac d.damyanov 4.pptxIac d.damyanov 4.pptx
Iac d.damyanov 4.pptx
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning service
 
Social Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes DemystifiedSocial Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes Demystified
 
Make Your Build Great Again (DroidConSF 2017)
Make Your Build Great Again (DroidConSF 2017)Make Your Build Great Again (DroidConSF 2017)
Make Your Build Great Again (DroidConSF 2017)
 
PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part IPROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part I
 
Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11
 
Spring boot
Spring bootSpring boot
Spring boot
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
 
A to z for sql azure databases
A to z for sql azure databasesA to z for sql azure databases
A to z for sql azure databases
 
Blue Green Sitecore Deployments on Azure
Blue Green Sitecore Deployments on AzureBlue Green Sitecore Deployments on Azure
Blue Green Sitecore Deployments on Azure
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC Applications
 
os-php-wiki5-a4
os-php-wiki5-a4os-php-wiki5-a4
os-php-wiki5-a4
 
os-php-wiki5-a4
os-php-wiki5-a4os-php-wiki5-a4
os-php-wiki5-a4
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Sa106 – practical solutions for connections administrators
Sa106 – practical solutions for connections administratorsSa106 – practical solutions for connections administrators
Sa106 – practical solutions for connections administrators
 
DrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizingDrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizing
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 

Recently uploaded

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

Grails Connecting to MySQL

  • 1. Grails Tutorial Connecting to MySql
  • 3. • By default, Grails applications are configured to use an in-memory HSQLDB database for development and testing • Now we will create application to store student information in database. • Create 2 new databases that will be used by our application • create database student_registration; • create database student_registration_prod;
  • 5. • After creating the database, the next thing to do is to create a new application. • grails create-app student-registraion • After creating the application, the next step is to setup the its dependencies. There are two ways of setting up the application to use MySQL database, However, I prefer this way since it is much easier. •Inside the file BuildConfig.groovy file under grails- app/conf, find and uncomment the line which says: • //runtime 'mysql:mysql-connector-java:5.1.16' • As of this writing, the latest versioin of the JDBC is 5.1.18, so update accordingly.
  • 6. • You also need to uncomment maven repository if it is commented (mavenCentral()) • Note : if application failed to start, then add mysql- connector-java-5.1.18-bin.jar in lib directory • Now we are ready to add database details. We need to Update DataSource.groovy file under grails- app/conf. dataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" }
  • 7. • We need to change the driverClassName to point to the proper class. • Also, since we are using the InnoDB engine, we will be setting the dialect dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" • Note : If application failed to start bcoz of database credentials add the 2 entries after dialect • username=“username” • password=“password”
  • 8. • Next step just shows you how to configure environment variable for database • Let us use the two databases we created a while ago. • For development & test environment, let us use the student_registration database. On the other hand, for the production environment, let us use the student_registration_prod database.
  • 9. For Development & Test use same datasource development { dataSource { dbCreate = "create-drop" url = "jdbc:mysql://localhost/student_registration? useUnicode=yes&characterEncoding=UTF-8" username = "root“ password = "" } } • Note for dbCreate read next slide
  • 10. For Production use same datasource production { dataSource { dbCreate = “update" url = "jdbc:mysql://localhost/student_registration? useUnicode=yes&characterEncoding=UTF-8" username = "root“ password = "" } }
  • 11. dbCreate •dbCreate deserves a special mention, It determines what happens to your database between server restarts. • For example, a value of create-drop means that the database tables will be dropped and then recreated when you start the server, so you lose any data that was in there. This is fine for testing, but is definitely not what you want in production
  • 12. Part 3 Create Domain class,Controller & Views
  • 13. •The next step is to create a domain class that will be persisted later in our database. Let’s create a sample domain class. • create-domain-class com.reg.Student • Now update the Student.groovy file in domain dir class Student { String studentID String firstName String middleName String lastName Integer year static constraints = { } }
  • 14. •The next step is to create a controller class • create-controller com.reg.Student • Update the file StudentController.groovy under grails-app/controller/com.reg/. • Inside the controller, we will define a scaffold which will automatically create the CRUD (Create, Read, Update, Delete) functionality for our domain class class StudentController { def scaffold = Student def index() { } }
  • 15. •The next step is to generate a view for Student • generate-views com.reg.Student • There is no need to change in view (.gsp) pages. The grails automatically creates all CRUD (Create,Read,Update,Delete) functionality for you. • You just need to care about the url mapping for all. After running your application if your controller id redirected to “index” then write “create” instead of “index”. • I don’t know where is url mapping configured in grails, that’s why I use this to redirect the pages.
  • 16. • Now its time to start your grails student-registration application • grails run-app • If your application starts without any errors, use the url printed on console to access your app. • Now connect to your student_registration database in mysql • connect student_registration; • Now you notice that new table student is created. • desc student;
  • 17. • Now access the url and check all CRUD (Create,Read,Update,Delete) functionality is working or not. • http://localhost:8080/student- registraion/student/create • Check the database values after each create,update & delete.
  • 18. Thank you • I am not expert in grails, but if you have any doubts in tutorial please feel free to contact me on ashishkirpan@gmail.com • Sorry for grammar mistakes :)