Grails Connecting to MySQL


Published on

Grails Tutorial for Connecting to MySql Database

Published in: Education, Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Grails Connecting to MySQL

  1. 1. Grails Tutorial Connecting to MySql
  2. 2. Part 1Creating database in MySql
  3. 3. • By default, Grails applications are configured touse an in-memory HSQLDB database fordevelopment and testing• Now we will create application to store studentinformation in database.• Create 2 new databases that will be used by ourapplication • create database student_registration; • create database student_registration_prod;
  4. 4. Part 2Create Application in Grails
  5. 5. • After creating the database, the next thing to do isto create a new application. • grails create-app student-registraion• After creating the application, the next step is tosetup the its dependencies. There are two ways ofsetting 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 is5.1.18, so update accordingly.
  6. 6. • You also need to uncomment maven repository if it iscommented (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 needto Update DataSource.groovy file under grails-app/conf. dataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" }
  7. 7. • We need to change the driverClassName to point tothe proper class.• Also, since we are using the InnoDB engine, we will besetting the dialect dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"• Note : If application failed to start bcoz of databasecredentials add the 2 entries after dialect • username=“username” • password=“password”
  8. 8. • Next step just shows you how to configureenvironment variable for database• Let us use the two databases we created a while ago.• For development & test environment, let us use thestudent_registration database. On the other hand, forthe production environment, let us use thestudent_registration_prod database.
  9. 9. For Development & Test use same datasourcedevelopment { 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. 10. For Production use same datasourceproduction { dataSource { dbCreate = “update" url ="jdbc:mysql://localhost/student_registration?useUnicode=yes&characterEncoding=UTF-8" username = "root“ password = "" } }
  11. 11. dbCreate•dbCreate deserves a special mention, It determineswhat happens to your database between serverrestarts.• For example, a value of create-drop means that thedatabase tables will be dropped and then recreatedwhen you start the server, so you lose any data thatwas in there. This is fine for testing, but is definitelynot what you want in production
  12. 12. Part 3Create Domain class,Controller & Views
  13. 13. •The next step is to create a domain class that will bepersisted later in our database. Let’s create a sampledomain 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. 14. •The next step is to create a controller class • create-controller com.reg.Student• Update the file StudentController.groovy undergrails-app/controller/com.reg/.• Inside the controller, we will define a scaffold whichwill automatically create the CRUD (Create, Read,Update, Delete) functionality for our domain class class StudentController { def scaffold = Student def index() { } }
  15. 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. Thegrails 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 idredirected 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. 16. • Now its time to start your grails student-registrationapplication • grails run-app• If your application starts without any errors, use theurl printed on console to access your app.• Now connect to your student_registration database inmysql • connect student_registration;• Now you notice that new table student is created. • desc student;
  17. 17. • Now access the url and check all CRUD(Create,Read,Update,Delete) functionality is working ornot. • http://localhost:8080/student- registraion/student/create• Check the database values after each create,update &delete.
  18. 18. Thank you• I am not expert in grails, but if you have any doubts intutorial please feel free to contact me• Sorry for grammar mistakes :)