当前位置:web 服务器 > Apache >>

Apache DBUtils:使用Bean(ORM)例子

Java代码 
import org.apache.commons.dbutils.DbUtils;    
import org.apache.commons.dbutils.QueryRunner;    
import org.apache.commons.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);    
  }    
}   
 作者“天道酬勤”
 

补充:软件开发 , Java ,
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
访问www.zzzyk.com 试试
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,