---
title: 'Putziger kleiner OR-Mapper: ORMLite, für JDBC und auch für Android'
url: 'https://www.tutego.de/blog/javainsel/2012/07/putziger-kleiner-or-mapper-ormlite'
markdown: 'https://www.tutego.de/blog/javainsel/2012/07/putziger-kleiner-or-mapper-ormlite.md'
date: '2012-07-04'
description: 'package tutego; import java.sql.SQLException; import com.j256.ormlite.dao.*; import com.j256.ormlite.db.HsqldbDatabaseType; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.jdbc.JdbcConnectionSource; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.tabl…'
taxonomy:
  category:
    - 'Open Source'
    - HSQLDB
    - ORMLite
---

# Putziger kleiner OR-Mapper: ORMLite, für JDBC und auch für Android

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://www.tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://www.tutego.de/blog/javainsel/)

## Putziger kleiner OR-Mapper: ORMLite, für JDBC und auch für Android

04.07.2012 · [Christian Ullenboom](https://www.tutego.de/blog/javainsel/author/admin/)

 ```java
package tutego;

import java.sql.SQLException;
import com.j256.ormlite.dao.*;
import com.j256.ormlite.db.HsqldbDatabaseType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.*;

@DatabaseTable
class Contact
{
  @DatabaseField( generatedId = true )
  Long id;

  @DatabaseField
  String name;

  // Setter/Getter sparen
}

public class ORMLiteDemo
{
  public static void main( String[] args )
  {
    System.setProperty( "tutegoHsqldbDatabasePath", "TutegoDB" );
    String url = "jdbc:hsqldb:file:${tutegoHsqldbDatabasePath};shutdown=true";
    ConnectionSource connectionSource;
    try {
      connectionSource = new JdbcConnectionSource( url, "sa", "", new HsqldbDatabaseType() );
      Dao dao = DaoManager.createDao( connectionSource, Contact.class );
//      TableUtils.createTable( connectionSource, Contact.class );

      Contact c1 = new Contact();
      c1.name = "Chris";
      dao.create( c1 );
      Contact c2 = new Contact();
      c2.name = "Juvy";
      dao.create( c2 );

      Contact c3 = dao.queryForId( "1" );
      System.out.println( c3.name );
      connectionSource.close();
    }
    catch ( SQLException e ) {
      e.printStackTrace();
    }
  }
}
```

1\. Klassen annotieren mit den ORMLite-Annotationen oder mit JPA-Annotationen

2\. Sind die Tabellen nicht da, muss man `TableUtils.createTable( connectionSource, Contact.class )`; aufrufen, dann erzeugt ORMLite die Tabellen.

3\. Der Rest ist einfach, siehe Beispiel 🙂

Mehr unter .

Kategorien: [Open Source](https://www.tutego.de/blog/javainsel/category/open-source/), [HSQLDB](https://www.tutego.de/blog/javainsel/category/hsqldb/), [ORMLite](https://www.tutego.de/blog/javainsel/category/ormlite/)

[← Alle Beiträge](https://www.tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Neues Eclipse Release: 4.2 (Juno) ist fertig](https://www.tutego.de/blog/javainsel/2012/07/neues-eclipse-release-4-2-junor-ist-fertig.md)
- Next: [Java und Sequenzpunkte in C(++)](https://www.tutego.de/blog/javainsel/2012/07/java-und-sequenzpunkte-in-c.md)
