Java/Database SQL JDBC/Apache Dbutils

Материал из Java эксперт
Версия от 06:34, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

DbUtils:Use Bean (ORM)

import org.apache.rumons.dbutils.DbUtils;
import org.apache.rumons.dbutils.QueryRunner;
import org.apache.rumons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
public class DbUtilsUseBeanMySQL {
  public static void main(String[] args) {
    Connection conn = null;
    String jdbcURL = "jdbc:mysql://localhost/octopus";
    String jdbcDriver = "com.mysql.jdbc.Driver";
    String user = "root";
    String password = "root";
    try {
      DbUtils.loadDriver(jdbcDriver);
      conn = DriverManager.getConnection(jdbcURL, user, password);
      QueryRunner qRunner = new QueryRunner();
      List beans = (List) qRunner.query(conn, "select id, name from animals_table",
          new BeanListHandler(Employee.class));
      for (int i = 0; i < beans.size(); i++) {
        Employee bean = (Employee) beans.get(i);
        bean.print();
      }
    } catch (SQLException e) {
      // handle the exception
      e.printStackTrace();
    } finally {
      DbUtils.closeQuietly(conn);
    }
  }
}
class Employee {
  private int id;
  private String name;
  public Employee() {
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getName() {
      return this.name;
  }
  public void setId(int id) {
      this.id = id;
  }
  public int getId() {
      return this.id;
  }
  public void print() {
      System.out.println("id="+id+" name="+name);
  }
}





DbUtils:Use Map against MySQL

import org.apache.rumons.dbutils.DbUtils;
import org.apache.rumons.dbutils.QueryRunner;
import org.apache.rumons.dbutils.handlers.MapListHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.List;
public class DbUtilsUseMapMySQL {
  public static void main(String[] args) {
    Connection conn = null;
    String jdbcURL = "jdbc:mysql://localhost/octopus";
    String jdbcDriver = "com.mysql.jdbc.Driver";
    String user = "root";
    String password = "root";
    try {
      DbUtils.loadDriver(jdbcDriver);
      conn = DriverManager.getConnection(jdbcURL, user, password);
      QueryRunner qRunner = new QueryRunner();
      List mapList = (List) qRunner.query(conn, "select id, name from animals_table",
          new MapListHandler());
      for (int i = 0; i < mapList.size(); i++) {
        Map map = (Map) mapList.get(i);
        System.out.println("id=" + map.get("id"));
        System.out.println("name=" + map.get("name"));
        System.out.println("-----------------");
      }
      System.out.println("DbUtils_UseMap_MySQL: end.");
    } catch (SQLException e) {
      // handle the exception
      e.printStackTrace();
    } finally {
      DbUtils.closeQuietly(conn);
    }
  }
}
class Employee {
  private int id;
  private String name;
  public Employee() {
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getName() {
    return this.name;
  }
  public void setId(int id) {
    this.id = id;
  }
  public int getId() {
    return this.id;
  }
  public void print() {
    System.out.println("id=" + id + " name=" + name);
  }
}