<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Netbeans and Derby and Ajax</title>
<meta name="AUTHOR" content="Jose M Vidal"></meta>
<meta name="GENERATOR" content="xslides.el, written by J.M. Vidal"></meta>
<link rel="next" href="directwebremoting.xml" title="Direct Web Remoting"/>
<link rel="previous" href="convertkeyexamplewithjson.xml"  title="Convert Key Example with JSON"/>
<link rel="author" href="../../index.html" title="Jose M Vidal"/>
<link rel="parent" href="index.xml" title="Index"/>
<link rel="top" href="../index.html" title="Presentation List"/>

<link rel="stylesheet" type="text/css" href="../xslides-medwhite.css" title="White" media="all" />
 	    <link rel="alternate stylesheet" type="text/css" href="../xslides-medblue.css" title="Blue" media="all" />
 	    <link rel="alternate stylesheet" type="text/css" href="../xslides-usc.css" title="USCTheme" media="all" />
<script type="text/javascript" src="../scripts/stylehelper.js"></script>
<link rel="stylesheet" href="../lightbox.css" type="text/css" media="screen"/>
</head>
<body>
<div class="navbar">
<p>
<a onclick="javascript:this.href=getURL('convertkeyexamplewithjson.xml');return true;" rel="prev" href="convertkeyexamplewithjson.xml" accesskey="P" title="Convert Key Example with JSON">&larr;</a>
<a rel="index" href="index.xml" onclick="javascript:this.href=getURL('index.xml');return true;" accesskey="I" title="Index Page">^</a>
<a rel="next" href="directwebremoting.xml" onclick="javascript:this.href=getURL('directwebremoting.xml');return true;" accesskey="N" title="Direct Web Remoting">&rarr;</a>
</p>
</div>
<div class="talktitle"><p>AJAX Tricks and Prototype</p></div>
<h1 class="slide">Netbeans and Derby and Ajax</h1>
<div class="slidebody">
      <ul>
	<li>First, set up your database. </li>

	<li>Netbeans already runs a database called <a class="remote" href="http://db.apache.org/derby/docs/dev/getstart/">derby</a>. Follow
	this <a class="remote" href="http://www.netbeans.org/kb/55/derby-demo.html">tutorial</a>
	to create a database and connect it. </li>

	<li>Some tips on derby with netbeans: </li>
      </ul>
      <ol>
	<li>Add the <code>derby.jar</code> library to your project. It
	was installed under <code>glassfish-v2/javadb/lib</code>.</li>

	<li>Here is sample code for the connection:

	  <pre>
<span class="comment">/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */</span>

<span class="keyword">package</span> edu.sc.<span class="reference">cse</span>;

<span class="keyword">import</span> <span class="reference">org</span>.<span class="reference">apache</span>.<span class="reference">derby</span>.<span class="reference">jdbc</span>.*;

<span class="keyword">import</span> <span class="reference">java</span>.<span class="reference">sql</span>.<span class="type">Connection</span>;
<span class="keyword">import</span> <span class="reference">java</span>.<span class="reference">sql</span>.<span class="type">DriverManager</span>;

<span class="keyword">public</span> <span class="keyword">class</span> <span class="type">DatabaseConnector</span> {

    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">Connection</span> <span class="function-name">getConnection</span>() {
        <span class="type">Connection</span> <span class="variable-name">con</span> = null;
        <span class="type">String</span> <span class="variable-name">driver</span> = "<span class="string">org.apache.derby.jdbc.EmbeddedDriver</span>";
        <span class="keyword">try</span> {
            Class.forName(driver).newInstance();

        } <span class="keyword">catch</span> (<span class="type">Exception</span> <span class="variable-name">e</span>) {
            System.out.println("<span class="string">Failed to load mySQL driver.</span>");
            <span class="keyword">return</span> null;
        }
        <span class="keyword">try</span> {
            <span class="comment">//AJAX is the database name. 
</span>            <span class="comment">//This is for empty username and password
</span>            <span class="comment">//Otherwise append ?username=jmvidal&amp;password=usc
</span>            con = DriverManager.getConnection("<span class="string">jdbc:derby://localhost:1527/AJAX</span>");

          } <span class="keyword">catch</span> (<span class="type">Exception</span> <span class="variable-name">e</span>) {
              e.printStackTrace();
          }
          <span class="keyword">return</span> con;
      }
}

</pre> </li>

	<li>To import or export a whole table, like <a href="ch05-suggest/zipcodes.del">zipcodes.del</a> use:
	  <pre>
<span class="comment">/*To import my zipcodes.del table */</span>
<span class="keyword">CALL</span> SYSCS_UTIL.SYSCS_IMPORT_TABLE
(<span class="keyword">null</span>,'<span class="string">ZIPCODES</span>','<span class="string">/full-path-name/zipcodes.del</span>',<span class="keyword">null</span>,<span class="keyword">null</span>,<span class="keyword">null</span>,0)


<span class="comment">/* To export a table */</span>
<span class="keyword">CALL</span> SYSCS_UTIL.SYSCS_EXPORT_TABLE
(<span class="keyword">null</span>,'<span class="string">ZIPCODES</span>','<span class="string">/full-path-name/zipcodes.del</span>',<span class="keyword">null</span>,<span class="keyword">null</span>,<span class="keyword">null</span>)



</pre>
 </li>

	<li>To delete all rows use <code>delete from
	"TABLENAME"</code> </li>

	<li>To insert use <code>INSERT INTO TABLE (USERNAME,PASSWORD)
	VALUES ('jmvidal','usc')</code> </li>

	<li>When building statments in JDBC, <em>do not add a ; at the end</em>.</li>

      </ol>

      <ul>
	<li>Demo: <a href="c5/">zipcode and name completion netbeans project</a>, (<a href="c5/c5.zip">zipped</a>).</li>

      </ul>
    </div><hr class="bottom"/>
<p class="author"><a href="../../index.html">Jos&eacute; M. Vidal</a>
<a href=" http://validator.w3.org/check?uri=http://jmvidal.cse.sc.edu/talks/ajax/netbeansderby.xml">.</a></p>
<p class="pagenumber">6 of 16</p>
</body>
</html>