Using JavaDB from Processing

Nikolaus Gradwohl2011-04-27T05:01:35+00:00

JDBC is the java api for connecting to sql databases. Since java6, javadb - a small, simple, javabased database-engine is delivered with java. This is a small example of how to access a database with javaDB from a processing-sketch.

First you need to download the driver from the javadb download site select the "All Plattforms" package and extract derby.jar from the zip file.

Add this jar file to your processing-sketch using the "Add File..."-Item form the "Sketch"-Menu.

The following sketch creates a small database containing one table in the setup method (the "drop table ..." statement deletes the table if it already exists. If you want to pregenerate your database or keep it, remove this line.)

In the draw method the foo table gets queried and the "name" column of each row gets printed on the console

import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;

Connection con;
void setup() {
  try {
    // Tell derby where to put the database files
    System.setProperty("derby.system.home", sketchPath("test"));

    // tell java which database-driver to use
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

    // create a database named test
    con = DriverManager.getConnection("jdbc:derby:test;create=true");
    Statement stmt = con.createStatement();

    try {
        // drop the foo table if it exists
        stmt.execute("drop table foo");
    } catch (Exception e ) {
    }
    // create a table named foo and insert two rows
    stmt.execute("create table foo ( id integer not null, name varchar(20))");
    stmt.executeUpdate("insert into foo values (0, 'foo')");
    stmt.executeUpdate("insert into foo values (1, 'bar')");
  } 
  catch (Exception e ) {
    e.printStackTrace();
  }
  noLoop();
}

void draw() {
  try {
    // select all rows from the foo table
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from foo");
    while( rs.next()) {
      // print the content of the "name" column on the console
      println(rs.getString("name"));
    }
  } 
  catch( Exception e ) {
    e.printStackTrace();
  }
}
Tweet This! submit to reddit Digg! Tags: | 1 comments | no trackbacks

See also:

sketch experiment 7 - osc events
nested cubes in processing
processing phaseflower
Time Perception
de jong attractor

Trackbacks

Comments

Leave a response

  1. Fropfoods 2013-08-04T15:12:41+00:00
    手提げ袋は付属しておりません。約 34cm 仕様:トンプソン コンバーチブル スリング パック/ボディバッグ 新作モデル!高級感漂う上質なレザーを使用したスタイリッシュなデザインがオシャレな新作ボディバッグです?
    &#12461;&#12540;&#12507;&#12523;&#12480;&#12540;&#12289;&#12501;&#12449;&#12473;&#12490;&#12540;&#38283;&#38281; &#20869;&#20596;&#65306;&#12501;&#12449;&#12473;&#12490;&#12540;&#12509;&#12465;&#12483;&#12488;1&#12289; <a href=http://www.katespadetoyou.com/c-133.html>&#12465;&#12452;&#12488;&#12473;&#12506;&#12540;&#12489; &#12381;&#12398;&#20182;&#12496;&#12483;&#12464; &#12450;&#12454;&#12488;&#12524;&#12483;&#12488;</a>
    

    カード入れ COACHアメリカ正規店より直輸入 並行輸入品の為、? もしあなたが損なわれた力が使ったよりずっと容易なオプションになることを望むなら、商品情報? 少々の型崩れ、ファスナーポケット1、ファスナーポケット外側:!

Leave a comment