<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Altentee » Performance &#38; Test Automation Experts &#187; jdbc</title>
	<atom:link href="http://altentee.com/blogs/tag/jdbc/feed/" rel="self" type="application/rss+xml" />
	<link>http://altentee.com</link>
	<description>Performance and Test Automation Experts</description>
	<lastBuildDate>Fri, 04 Nov 2011 03:17:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Dynamic Data from JMeter JDBC Requests</title>
		<link>http://altentee.com/blogs/2010/dynamic-data-from-jmeter-jdbc-requests/</link>
		<comments>http://altentee.com/blogs/2010/dynamic-data-from-jmeter-jdbc-requests/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 06:45:17 +0000</pubDate>
		<dc:creator>Tim Koopmans</dc:creator>
				<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jmeter]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://altentee.com/?p=731</guid>
		<description><![CDATA[A common requirement when load testing is to populate subsequent requests with dynamic data from previous requests. If you&#8217;re thinking about using a database to store static test data, such as usernames, paswords, credit card numbers etc then read on to find out how to extract this data at runtime and populate JMeter variables. In [...]]]></description>
			<content:encoded><![CDATA[<p>A common requirement when load testing is to populate subsequent requests with dynamic data from previous requests. If you&#8217;re thinking about using a database to store static test data, such as usernames, paswords, credit card numbers etc then read on to find out how to extract this data at runtime and populate JMeter variables.</p>
<p><span id="more-731"></span>In this example we&#8217;ll need to add a JDBC Connection Configuration to our test plan. This will define and control the way in which JMeter will access your database.</p>
<p><a href="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.28.18-PM.png"><img class="alignleft size-full wp-image-732" title="Screen shot 2010-01-05 at 5.28.18 PM" src="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.28.18-PM.png" alt="" width="647" height="423" /></a></p>
<p>Notice I&#8217;ve left the defaults for connection pool properties, although you may need to tweak this depending on how you&#8217;ll access the data. I&#8217;ve used a JDBC connection URL that accesses a local mysql server listening on port 3306. The database in use is called watirgrid</p>
<p>I&#8217;ve also defined the JDBC Driver class and specified the username / password to connect with.</p>
<p>Now we can make a JDBC request using the JDBC Request sampler. It will look something like this:<a href="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.31.52-PM.png"><img class="alignleft size-full wp-image-733" title="Screen shot 2010-01-05 at 5.31.52 PM" src="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.31.52-PM.png" alt="" width="646" height="442" /></a></p>
<p>The Variable Name matches the JDBC Connection Config Variable Name i.e. mysql</p>
<p>I&#8217;m using a select statement that will print out results in a JSON-like format. That query is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
CONCAT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;[&quot;</span><span style="color: #66cc66;">,</span>
GROUP_CONCAT<span style="color: #66cc66;">&#40;</span>
CONCAT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;{email:'&quot;</span><span style="color: #66cc66;">,</span>email<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
CONCAT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;,login_count:'&quot;</span><span style="color: #66cc66;">,</span>login_count<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;'}&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;]&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span> json <span style="color: #993333; font-weight: bold;">FROM</span> users</pre></div></div>

<p>In doing this, it will return a result set that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------------------------------------+</span>
<span style="color: #66cc66;">|</span> json                                            <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------------------------------------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span>email:<span style="color: #ff0000;">'tim.koops@gmail.com'</span><span style="color: #66cc66;">,</span>login_count:<span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------------------------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now we need to add a Post Processor Regex Extractor to the JDBC request as follows:</p>
<p><a href="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.34.39-PM.png"><img class="alignleft size-full wp-image-734" title="Screen shot 2010-01-05 at 5.34.39 PM" src="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.34.39-PM.png" alt="" width="172" height="40" /></a></p>
<p>The reason we do this is so we can extract the results from the JSON string returned by the database query. For example, to extract and populate a variable called ${email} our PPRE would look like this:</p>
<p><a href="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.36.45-PM.png"><img class="alignleft size-full wp-image-735" title="Screen shot 2010-01-05 at 5.36.45 PM" src="http://altentee.com/blogs/wp-content/uploads/2010/01/Screen-shot-2010-01-05-at-5.36.45-PM.png" alt="" width="652" height="236" /></a></p>
<p>Now we&#8217;re cooking! Add a Debug Sampler to confirm everything works as you&#8217;d expect. You should see results like this in the response data</p>
<pre>email=tim.koops@gmail.com
email_g=1
email_g0=email:'tim.koops@gmail.com'
email_g1=tim.koops@gmail.com
mysql=org.apache.jmeter.protocol.jdbc.config.DataSourceElement$DataSourceComponentImpl@10d69502
</pre>
<p>Attached is a demo test plan: <a href="http://altentee.com/blogs/wp-content/uploads/2010/01/jdbc_example.jmx_.zip">jdbc_example.jmx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://altentee.com/blogs/2010/dynamic-data-from-jmeter-jdbc-requests/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using JDBC with LoadRunner Java Vusers</title>
		<link>http://altentee.com/blogs/2008/using-jdbc-with-loadrunner-java-vusers/</link>
		<comments>http://altentee.com/blogs/2008/using-jdbc-with-loadrunner-java-vusers/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 04:39:06 +0000</pubDate>
		<dc:creator>Tim Koopmans</dc:creator>
				<category><![CDATA[Altentee]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[loadrunner]]></category>

		<guid isPermaLink="false">http://90kts.com/blog/2008/using-jdbc-with-loadrunner-java-vusers/</guid>
		<description><![CDATA[This is a short post on how to establish JDBC connectivity to a MySQL database when using the Java Vuser type within LoadRunner. Stuart describes a situation in which you might want to do this here. I&#8217;m not sure why but the example code is missing from his site, so I&#8217;ve supplemented with an example [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short post on how to establish JDBC connectivity to a MySQL database when using the Java Vuser type within LoadRunner. Stuart describes a situation in which you might want to do this <a href="http://www.myloadtest.com/connecting-to-a-mysql-database-with-loadrunner/">here</a>. I&#8217;m not sure why but the example code is missing from his site, so I&#8217;ve supplemented with an example of how you&#8217;d implement this within LoadRunner.<br />
<span id="more-147"></span><br />
The first thing you will need is the JDBC driver available <a href="http://dev.mysql.com/downloads/connector/j/5.1.html">here</a>. Once you download the archive, you really just need the one jar file from that source. I&#8217;m currently using the file called <strong>mysql-connector-java-5.0.8-bin.jar</strong></p>
<p>The next thing to do is copy that file into your LoadRunner <code><installdir>/classes</code> subdirectory.</p>
<p>Now edit the run-time settings for your Java Vuser script (F4). You need to add the location of the jar file to your classpath parameters.<br />
<a href='http://90kts.com/blog/wp-content/uploads/2008/06/classdir.jpg' title='Classpath for Java Vuser'><img src='http://90kts.com/blog/wp-content/uploads/2008/06/classdir.jpg' alt='Classpath for Java Vuser' /></a></p>
<p>At the top of your script, depending on which functionality you end up using, you need to import the relevant packages for your script. I&#8217;m currently using the following import statements:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">import java.<span style="color: #202020;">sql</span>.<span style="color: #202020;">Connection</span><span style="color: #339933;">;</span>
import java.<span style="color: #202020;">sql</span>.<span style="color: #202020;">DriverManager</span><span style="color: #339933;">;</span>
import java.<span style="color: #202020;">sql</span>.<span style="color: #202020;">SQLException</span><span style="color: #339933;">;</span>
import java.<span style="color: #202020;">sql</span>.<span style="color: #339933;">*;</span></pre></td></tr></table></div>

<p>My public class called Actions now initializes a private Connection object called &#8216;connection&#8217;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">private Connection connection<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>My init() method will open up a single connection to the database using the following code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Initialize DB connection</span>
	connection <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	try <span style="color: #009900;">&#123;</span>
	    Class.<span style="color: #202020;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #202020;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
	try <span style="color: #009900;">&#123;</span>
	    connection <span style="color: #339933;">=</span>  DriverManager.<span style="color: #202020;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;jdbc:mysql://127.0.0.1:3306/mydb?&quot;</span> <span style="color: #339933;">+</span>
						      <span style="color: #ff0000;">&quot;user=myuser&amp;password=mypassword&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>SQLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You may want to handle exceptions a little more thoroughly than me <img src='http://altentee.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Now within the action() method, I can make repeated calls to the MySQL database, as each vuser iterates over the code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">String FORMATTED_MESSAGE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	Statement stmt 	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	ResultSet rs 	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	try <span style="color: #009900;">&#123;</span>
	    stmt <span style="color: #339933;">=</span> connection.<span style="color: #202020;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    rs   <span style="color: #339933;">=</span> stmt.<span style="color: #202020;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT xml FROM my_data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>rs.<span style="color: #202020;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		FORMATTED_MESSAGE <span style="color: #339933;">=</span> rs.<span style="color: #202020;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//lr.message(FORMATTED_MESSAGE);</span>
	    <span style="color: #009900;">&#125;</span>
	    rs.<span style="color: #202020;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    stmt.<span style="color: #202020;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>SQLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In this example I&#8217;m populating a string called FORMATTED_MESSAGE with the XML returned from a SQL query.</p>
<p>Putting it all together, your Java Vuser script should look something like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">import lrapi.<span style="color: #202020;">lr</span><span style="color: #339933;">;</span>
import java.<span style="color: #202020;">sql</span>.<span style="color: #202020;">Connection</span><span style="color: #339933;">;</span>
import java.<span style="color: #202020;">sql</span>.<span style="color: #202020;">DriverManager</span><span style="color: #339933;">;</span>
import java.<span style="color: #202020;">sql</span>.<span style="color: #202020;">SQLException</span><span style="color: #339933;">;</span>
import java.<span style="color: #202020;">sql</span>.<span style="color: #339933;">*;</span>
&nbsp;
public class Actions
<span style="color: #009900;">&#123;</span>
    private Connection connection<span style="color: #339933;">;</span>
&nbsp;
    public <span style="color: #993333;">int</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Initialize DB connection</span>
        connection <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        try <span style="color: #009900;">&#123;</span>
            Class.<span style="color: #202020;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #202020;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
        try <span style="color: #009900;">&#123;</span>
            connection <span style="color: #339933;">=</span>  DriverManager.<span style="color: #202020;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;jdbc:mysql://127.0.0.1:3306/mydb?&quot;</span> <span style="color: #339933;">+</span>
                                  <span style="color: #ff0000;">&quot;user=myuser&amp;password=mypassword&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>SQLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
	    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">//end of init</span>
&nbsp;
    public <span style="color: #993333;">int</span> action<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws JMSException <span style="color: #009900;">&#123;</span>
        String FORMATTED_MESSAGE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        Statement stmt 	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        ResultSet rs 	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        try <span style="color: #009900;">&#123;</span>
            stmt <span style="color: #339933;">=</span> connection.<span style="color: #202020;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            rs   <span style="color: #339933;">=</span> stmt.<span style="color: #202020;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT xml FROM mydata&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>rs.<span style="color: #202020;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                FORMATTED_MESSAGE <span style="color: #339933;">=</span> rs.<span style="color: #202020;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">//lr.message(FORMATTED_MESSAGE);</span>
            <span style="color: #009900;">&#125;</span>
            rs.<span style="color: #202020;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            stmt.<span style="color: #202020;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>SQLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009900;">&#125;</span>
	    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">//end of action</span>
&nbsp;
    public <span style="color: #993333;">int</span> end<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws JMSException <span style="color: #009900;">&#123;</span>
	    connection <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">//end of end</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://altentee.com/blogs/2008/using-jdbc-with-loadrunner-java-vusers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

