aspose file tools*
The moose likes Groovy and the fly likes How to retrive information from database using queries in class files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "How to retrive information from database using queries in class files" Watch "How to retrive information from database using queries in class files" New topic
Author

How to retrive information from database using queries in class files

Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

Hi all,

How to retrive information from database using queries in class files. Can i directly write queries in class files for grails or do we have some special way to do it.
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

Means you have a class named class Queries { def q = "select * from mytable" }
you want to use this q variable or what..??? question not clear..???


| B.E IT | SCJP 6.0 98 % |
Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

exactly !... How i will get that query to execute now
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73



This way i am doing
Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

As am using Netbeans to develop , it shows me an error in the line
def sql = Sql.newInstance("jdbc:,mysql://localhost:3306/myapp", "root", "yourpwd", "com.mysql.jdbc.Driver")
saying unable to resolve class Sql even after i had import the
import groovy.sql.Sql
file. Do i need to add any library. Also instead of giving the whole url path, username and password of the sql, why cant i give the "datasource" as an instance. Do that grails know the path of datasource configured in datasource.groovy file
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

Actually i am beginner. i am also using netbeans i dont know how to use that datasource in .groovy file
I thought this is basic so will help you, you need to import groovy.sql somthing jar file in your library. I too had that problem now it is solved
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

Ashish Agre wrote:
this is correct line remove , from above that is a mistake
Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

I tried it already ashish, How many files you will keep on giving that path in all class files. Wont it reduce the performance of application. surely it will. Instead of giving path each time just give the name "datasource" alone as a reference to it. This will be good one but unfortunately its not recognizing the reference and i donno why its so..
for example:
Instead of ,
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/myapp", "root", "yourpwd", "com.mysql.jdbc.Driver")


can give like,
def sql = Sql(datasource)


Where this datasource is what we configured already in datasource.groovy file
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

i said it was just for basics. Have tried this line of code you just gave me is it working fine or not ???
Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

ya i already worked out it. but yet not getting the way what i said.. If i get the prob surely ill let you know how it worked
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

use variable : static dataSource

then try
def sql = Sql.newInstance(dataSource) //
Class java.sql.SQLException
Message No suitable driver found for com.mysql.jdbc.Driver

This is the error can you figure out
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

This might help us ;)

Ref : http://stackoverflow.com/questions/425294/sql-database-views-in-grails
Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

Do you get the error like
Message No suitable driver found for com.mysql.jdbc.Driver
if so include the mysql connector jar file in the library of your application the problem will get solved. I have seen that link earlier with that only i got to know about directly giving reference to sql but when i use the same method it doesnt know what is that datasource and also asking to resolve the sql class.
def sql = Sql(datasource)


May be any experts or Moderator have to help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrive information from database using queries in class files
 
Similar Threads
storing and retriveing image through Ms-sql
How do I connect to database
Store and Retrive audio files from sql server using JSP
how to get latest object from database instead of changed object.
Struts