[prev in list] [next in list] [prev in thread] [next in thread] 

List:       grinder-use
Subject:    Re: [Grinder-use] MySQL and Python location
From:       "Noel O'Brien" <nobrien () newbay ! com>
Date:       2008-11-04 9:07:47
Message-ID: 200811040907.47507.nobrien () newbay ! com
[Download RAW message or body]

Since Grinder uses Jython, you could also use JDBC to access MySQL. Just 
download the MySQL JDBC Driver (.jar file) from MySQL and drop it into your 
classpath.

from net.grinder.script.Grinder import grinder
from java.sql import DriverManager
from com.sun.rowset import CachedRowSetImpl
from com.mysql.jdbc import Driver

## Properties
DEBUG = grinder.properties.getProperty("sng.debug", "true")
DB_URL = 
grinder.properties.getProperty("sng.db.url", "jdbc:mysql://localhost:3306/sng?user=sng&password=sng")

## Grinder "breadcrumbs"
log = grinder.logger.output
err = grinder.logger.error
out = grinder.logger.TERMINAL

# Load the MySQL JDBC driver.
DriverManager.registerDriver(Driver())

##
## Function to get a connection to the database
##
def getDBConnection():
    return DriverManager.getConnection(DB_URL)


##
## Function to ensure the connection to the database is closed
##
def ensureDBClosed(object):
    try: object.close()
    except: pass


##
## Function to execute a database transaction
##
def doDBTransaction(query):

	if DEBUG == 'true':
		log(query)
	
	connection = None
	statement = None
	
	try:
		connection = getDBConnection()
		statement = connection.createStatement()

		if query.upper().startswith("SELECT"):
			rs = statement.executeQuery(query)
			crs = CachedRowSetImpl()		## A normal result set object would be closed 
when we closed the DB connection, so return a RS in memory
			crs.populate(rs)
			return crs
		else:
			return statement.execute(query)

	finally:
		ensureDBClosed(statement)
		ensureDBClosed(connection)


Regards,
Noel

On Monday 03 November 2008 22:49:52 Matthias Howell wrote:
> I'm using this bit:
>
> from com.ziclix.python.sql import zxJDBC
>
> args = ArrayList();
>
>  db = zxJDBC.connect("jdbc:mysql://host/dbname", "user", "password",
> "com.mysql.jdbc.Driver")
>     c = db.cursor()
>     c.execute("select statement goes here")
>     for row in c:
>         args.add(row[0])
>     c.close()
>     db.close()
>
> HTH
>
> Matthias
>
> On Mon, Nov 3, 2008 at 5:43 PM, Ron Brennan <brennan.ron@gmail.com> wrote:
> > Hello,
> >
> > I can use MySQLdb on my local machine, but when I try and access through
> > Grinder, it can't be found.  Where does Grinder look for libs?  I'd like
> > to be able to see if a simple move of the lib will fix the issue.  If
> > not, how are people working with databases?
> >
> > Thanks,
> > Ron
> >
> > -------------------------------------------------------------------------
> > This SF.Net email is sponsored by the Moblin Your Move Developer's
> > challenge
> > Build the coolest Linux based applications with Moblin SDK & win great
> > prizes
> > Grand prize is a trip for two to an Open Source event anywhere in the
> > world http://moblin-contest.org/redirect.php?banner_id=100&url=/
> > _______________________________________________
> > grinder-use mailing list
> > grinder-use@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/grinder-use



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
grinder-use mailing list
grinder-use@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/grinder-use
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic