ORMLite是一个轻量级对象关系映射持久层框架。ORMLite支持MySQL、Postgres、Microsoft SQL Server、H2、Derby、HSQLDB和Sqlite。提供灵活的QueryBuilder来构建复杂的查询。强大的抽象DAO类,让你的数据库读写类只需5行代码。能够自动生成SQL来创建和删除数据库表格。
https://github.com/j256/ormlite-jdbc
http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_7.html#Examples
package com.jiepu;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "CellPhone")
public class CellPhone {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
private String name;
@DatabaseField
private String company;
public CellPhone() {
}
public CellPhone(int id, String name, String company) {
super();
this.id = id;
this.name = name;
this.company = company;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return "*******" + name + " " + company + "*******";
}
}
package com.jiepu;
import java.sql.SQLException;
import java.util.List;
import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
public class CellPhoneDao extends BaseDaoImpl<CellPhone, String> implements
CellPhoneDaoInterface {
Dao<CellPhone, String> accountDao;
public CellPhoneDao(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, CellPhone.class);
}
public void performDBOperations(ConnectionSource connectionSource)
throws SQLException {
accountDao = DaoManager.createDao(connectionSource, CellPhone.class);
// create table
TableUtils.createTableIfNotExists(connectionSource, CellPhone.class);
CellPhone cp = new CellPhone();
cp.setName("N 95");
cp.setCompany("Nokia");
CellPhone cp1 = new CellPhone();
cp1.setName("N 96");
cp1.setCompany("android ");
CellPhone cp3 = new CellPhone();
cp3.setName("N 96");
cp3.setCompany("from 中国 ");
// save objects to DB
accountDao.create(cp);
accountDao.create(cp1);
accountDao.create(cp3);
// retrieve all objects from DB
List<CellPhone> list = accountDao.queryForAll();
System.out.println("*******List of objects saved in DB*******");
for (CellPhone cellPhone : list) {
System.out.println(cellPhone);
}
}
}
package com.jiepu;
import com.j256.ormlite.dao.Dao;
public interface CellPhoneDaoInterface extends Dao<CellPhone, String> {
}
package com.jiepu;
import java.sql.SQLException;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
public class CellPhoneDemo {
public static void main(String[] args) {
//testSqlite("jdbc:sqlite:data.db");
//TestMySql("jdbc:mysql://localhost/itcast");
//testDerby("jdbc:derby:ormlitederby;create=true");
testSqlServer2008("jdbc:jtds:sqlserver://10.0.0.111/java");
}
private static void testSqlServer2008(String databaseUrl) {
ConnectionSource connectionSource;
try {
connectionSource = new JdbcConnectionSource(databaseUrl);
((JdbcConnectionSource) connectionSource).setUsername("sa");
((JdbcConnectionSource) connectionSource)
.setPassword("xxx");
CellPhoneDao cd = new CellPhoneDao(connectionSource);
cd.performDBOperations(connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void testDerby(String databaseUrl) {
ConnectionSource connectionSource;
try {
connectionSource = new JdbcConnectionSource(databaseUrl);
((JdbcConnectionSource) connectionSource).setUsername("root");
((JdbcConnectionSource) connectionSource)
.setPassword("xxx");
CellPhoneDao cd = new CellPhoneDao(connectionSource);
cd.performDBOperations(connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
//记得先要下载相应的jdbc jar包
private static void TestMySql(String databaseUrl) {
ConnectionSource connectionSource;
try {
connectionSource = new JdbcConnectionSource(databaseUrl);
((JdbcConnectionSource) connectionSource).setUsername("root");
((JdbcConnectionSource) connectionSource)
.setPassword("xxx");
CellPhoneDao cd = new CellPhoneDao(connectionSource);
cd.performDBOperations(connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void testSqlite(String databaseUrl) {
// String connectionString = "jdbc:sqlite:data.db";
// String databaseUrl = "jdbc:mysql://localhost/itcast";
ConnectionSource connectionSource;
try {
connectionSource = new JdbcConnectionSource(databaseUrl);
((JdbcConnectionSource) connectionSource).setUsername("root");
((JdbcConnectionSource) connectionSource)
.setPassword("xxx");
CellPhoneDao cd = new CellPhoneDao(connectionSource);
cd.performDBOperations(connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
demo源码下载:
http://download.csdn.net/detail/earbao/8567249