Your SlideShare is downloading. ×
  • Like
Grails Connecting to MySQL
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Grails Connecting to MySQL

  • 2,814 views
Published

Grails Tutorial for Connecting to MySql Database

Grails Tutorial for Connecting to MySql Database

Published in Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,814
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
46
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Grails Tutorial Connecting to MySql
  • 2. Part 1Creating database in MySql
  • 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. Part 2Create Application in Grails
  • 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. • 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. • 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. • 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. 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. For Production use same datasourceproduction { 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 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. Part 3Create Domain class,Controller & Views
  • 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. •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. •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. • 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. • 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. Thank you• I am not expert in grails, but if you have any doubts intutorial please feel free to contact me onashishkirpan@gmail.com• Sorry for grammar mistakes :)