ormlite jdbc 使用例子

ORMLite是一个轻量级对象关系映射持久层框架。ORMLite支持MySQL、Postgres、Microsoft SQL Server、H2、Derby、HSQLDB和Sqlite。提供灵活的QueryBuilder来构建复杂的查询。强大的抽象DAO类,让你的数据库读写类只需5行代码。能够自动生成SQL来创建和删除数据库表格。

https://github.com/j256/ormlite-jdbc

http://ormlite.com/releases/

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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值