Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.j256.ormlite.examples.fieldConfig;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertNotNull;
  
  import java.util.Date;
  
Main sample routine to show how to do basic operations with the package.

NOTE: We use asserts in a couple of places to verify the results but if this were actual production code, we would have proper error handling.

 
 public class FieldConfigMain {
 
 	// we are using the in-memory H2 database
 	private final static String DATABASE_URL = "jdbc:h2:mem:account";
 
 	private Dao<AccountIntegeraccountDao;
 
 	public static void main(String[] argsthrows Exception {
 		// turn our static method into an instance of Main
 		new FieldConfigMain().doMain(args);
 	}
 
 	private void doMain(String[] argsthrows Exception {
 		JdbcConnectionSource connectionSource = null;
 		try {
 			// create our data-source for the database
 			connectionSource = new JdbcConnectionSource();
 			// setup our database and DAOs
 			setupDatabase(connectionSource);
 			// read and write some data
 			..println("\n\nIt seems to have worked\n\n");
 		} finally {
 			// destroy the data source which should close underlying connections
 			if (connectionSource != null) {
 				connectionSource.close();
 			}
 		}
 	}

Setup our database and DAOs
 
 	private void setupDatabase(ConnectionSource connectionSourcethrows Exception {
 
 		 = DaoManager.createDao(connectionSourceaccountTableConfig);
 
 		DatabaseTableConfig<DeliverydeliveryTableConfig = buildDeliveryTableConfig(accountTableConfig);
 		 = DaoManager.createDao(connectionSourcedeliveryTableConfig);
 
 		// if you need to create the table
 		TableUtils.createTable(connectionSourceaccountTableConfig);
 		TableUtils.createTable(connectionSourcedeliveryTableConfig);
 	}
 
 		DatabaseFieldConfig fieldConfig = new DatabaseFieldConfig("id");
 		fieldConfig.setGeneratedId(true);
 		fieldConfigs.add(fieldConfig);
 		fieldConfigs.add(new DatabaseFieldConfig("name"));
 		fieldConfig = new DatabaseFieldConfig("password");
 		fieldConfig.setCanBeNull(true);
 		fieldConfigs.add(fieldConfig);
 		DatabaseTableConfig<AccounttableConfig = new DatabaseTableConfig<Account>(Account.classfieldConfigs);
 		return tableConfig;
 	}
 
 		DatabaseFieldConfig fieldConfig = new DatabaseFieldConfig("id");
 		fieldConfig.setGeneratedId(true);
 		fieldConfigs.add(fieldConfig);
 		fieldConfigs.add(new DatabaseFieldConfig("when"));
 		fieldConfigs.add(new DatabaseFieldConfig("signedBy"));
 		fieldConfig = new DatabaseFieldConfig("account");
 		fieldConfig.setForeign(true);
 		fieldConfig.setForeignTableConfig(accountTableConfig);
 		fieldConfigs.add(fieldConfig);
 		DatabaseTableConfig<DeliverytableConfig = new DatabaseTableConfig<Delivery>(Delivery.classfieldConfigs);
 		return tableConfig;
 	}

Read and write some example data.
	private void readWriteData() throws Exception {
		// create an instance of Account
		String name = "Jim Coakley";
		Account account = new Account(name);
		// persist the account object to the database
		Delivery delivery = new Delivery(new Date(), "Mr. Ed"account);
		// persist the account object to the database
		Delivery delivery2 = .queryForId(delivery.getId());
		assertNotNull(delivery2);
		assertEquals(delivery.getId(), delivery2.getId());
		assertEquals(account.getId(), delivery2.getAccount().getId());
	}
New to GrepCode? Check out our FAQ X