Package com.j256.ormlite.support

Examples of com.j256.ormlite.support.ConnectionSource


  }

  User registerUser(String login, String fullName, String password) {
    User newUser = new User(login, fullName, password);
    Dao<User, Integer> usersDao;
    ConnectionSource connectionSource = null;
    User user = null;
    try {
      connectionSource = new JdbcConnectionSource(connectionString, userName, userPass);
      usersDao = DaoManager.createDao(connectionSource, User.class);
      QueryBuilder<User, Integer> queryBuilder = usersDao.queryBuilder();
      queryBuilder.where().like(User.LOGIN_FIELD_NAME, login).countOf();
      if (usersDao.countOf(queryBuilder.prepare()) > 0) {
        connectionSource.close();
        return null;
      }
      usersDao.create(newUser);
      connectionSource.close();
    } catch (SQLException ex) {
      System.err.println("Exception: " + ex.getMessage());
      ex.printStackTrace();
    }
    return newUser;
View Full Code Here


        table.setDataProvider(new DataProvider<Book>() {
         
      public List<Book> getData() {
            List<Book> books = null;
            try {
              ConnectionSource connectionSource =
                  (JdbcConnectionSource) GetConnection();
              books = book.queryForAll();
              connectionSource.close();
              return books;
            } catch (SQLException e) {
              e.printStackTrace();
            }
            return books;
View Full Code Here

      TextField fSupplier = (TextField)bFieldset.getControl("supplier");
      TextField fCkey = (TextField)bFieldset.getControl("classKey");
      TextField fClass = (TextField)bFieldset.getControl("classDescription");
      NumberField fQtty = (NumberField)bFieldset.getControl("quantity");
     
      ConnectionSource conn = (JdbcConnectionSource) GetConnection();
     
      Author a = new Author();
      if(fullNameArr.length > 1) {
        a = a.getByFullName(conn, author, fullNameArr);
        Integer author_id = a.getId();
        if (author_id != 0 && author_id != null) {
          if(fullNameArr.length > 2) {
            if (!a.getLname().equalsIgnoreCase(fullNameArr[1]) ||
                !a.getMname().equalsIgnoreCase(fullNameArr[2]) ||
                !a.getName().equalsIgnoreCase(fullNameArr[0]))
              author.update(a);
          } else if (fullNameArr.length == 2) {
            if (!a.getLname().equalsIgnoreCase(fullNameArr[1]) ||
                !a.getName().equalsIgnoreCase(fullNameArr[0]))
              author.update(a);
          }
          author.refresh(a);       
        } else {
          a = new Author(fullNameArr);
          author.create(a);
          author.refresh(a);
        }
      }
     
      Publisher p = new Publisher();
      if(!fPublisher.getValue().trim().isEmpty()) {
        p = p.getByName(conn, publisher,
            fPublisher.getValue().trim());
        Integer publisher_id = p.getId();
        if(publisher_id != 0 && publisher_id != null) {
          if(!p.getName().equalsIgnoreCase(fPublisher.getValue().trim()))
            publisher.update(p);
          publisher.refresh(p);
        } else {
          p = new Publisher(fPublisher.getValue().trim());
          publisher.create(p);
          publisher.refresh(p);
        }
      }
     
      Supplier s = new Supplier();
      if(!fSupplier.getValue().isEmpty()) {
        s.getByName(conn, supplier,
            fSupplier.getValue());
        Integer supplier_id = s.getId();
        if(supplier_id != 0 && supplier_id != null) {
          if(!s.getName().equalsIgnoreCase(fSupplier.getValue())) {
            supplier.update(s);
            supplier.refresh(s);
          }
        } else {
          s = new Supplier(fSupplier.getValue());
          supplier.create(s);
          supplier.refresh(s);
        }
      }
     
      Classification c = new Classification();
      if(!fCkey.getValue().isEmpty() &&
          !fClass.getValue().isEmpty()) {
        c.getByClassification(conn, classification,
            fCkey.getValue(), fClass.getValue());
        Integer class_id = c.getId();
        if(class_id != 0 && class_id != null) {
          if(!c.getDescription().equalsIgnoreCase(fClass.getValue())) {
            classification.update(c);
          }
          if(!c.getCkey().equalsIgnoreCase(fCkey.getValue())) {
            classification.update(c);
          }
          classification.refresh(c);
        } else {
          c = new Classification(fCkey.getValue(), fClass.getValue());
          classification.create(c);
          classification.refresh(c);
        }
      }
     
      Book b = new Book();
      form.copyTo(b);
      try {
        ISBN validIsbn = new ISBN(fIsbn.getValue());
        b.setIsbn(validIsbn.toString());
      } catch (InvalidStandardIDException e) {
        // TODO Auto-generated catch block
        b.setIsbn("");
        e.printStackTrace();
      }
      DateFormat df = new SimpleDateFormat("yyyy");
      Date year = df.parse(fPubDate.getSelectedValues().get(0).toString());
     
      if(year != null)
        b.setPubdate(year);
     
      if(a != null)
        b.setAuthor(a);
     
      if(b != null)
        b.setPublisher(p);
     
      if(c != null)
        b.setClassification(c);
     
      if(fQtty.getValue().isEmpty())
        b.setQuantity(1);
     
      Integer book_id = b.getId();
      if (book_id != 0 && book_id != null) {
        book.update(b);
      } else {
        book.create(b);
      }
     
      conn.close();
      form.clearValues();
      String msg = "New book added succesfully";
      addModel("msg", msg);
    }
    return true;
View Full Code Here

    // turn our static method into an instance of Main
    new SimpleMain().doMain(args);
  }

  private void doMain(String[] args) throws Exception {
    ConnectionSource connectionSource = null;
    try {
      // create our data-source for the database
      connectionSource = new JdbcConnectionSource(DATABASE_URL);
      // setup our database and DAOs
      setupDatabase(connectionSource);
      // read and write some data
      readWriteData();
      // do a bunch of bulk operations
      readWriteBunch();
      // show how to use the SelectArg object
      useSelectArgFeature();
      // show how to use the SelectArg object
      useTransactions(connectionSource);
      System.out.println("\n\nIt seems to have worked\n\n");
    } finally {
      // destroy the data source which should close underlying connections
      if (connectionSource != null) {
        connectionSource.close();
      }
    }
  }
View Full Code Here

    StatementExecutor<NoId, Void> se =
        new StatementExecutor<NoId, Void>(databaseType, new TableInfo<NoId, Void>(connectionSource, null,
            NoId.class), null);
    NoId noId = new NoId();
    noId.stuff = "1";
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getReadOnlyConnection()).andReturn(null);
    replay(connectionSource);
    se.delete(connectionSource.getReadOnlyConnection(), noId, null);
  }
View Full Code Here

  private final DatabaseType databaseType = new StubDatabaseType();

  @Test(expected = SQLException.class)
  public void testNoIdBuildDelete() throws Exception {
    DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    replay(connectionSource);
    MappedDeleteCollection.deleteObjects(databaseType,
        new TableInfo<NoId, Void>(connectionSource, null, NoId.class), databaseConnection,
        new ArrayList<NoId>(), null);
  }
View Full Code Here

  @SuppressWarnings("unchecked")
  @Test
  public void testForeignAutoRefresh() throws Exception {
    Field field = ForeignAutoRefresh.class.getDeclaredField("foreign");
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    DatabaseConnection connection = createMock(DatabaseConnection.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    expect(connectionSource.getReadOnlyConnection()).andReturn(connection);
    ForeignForeign foreignForeign = new ForeignForeign();
    String stuff = "21312j3213";
    int id = 4123123;
    foreignForeign.id = id;
    foreignForeign.stuff = stuff;
    expect(
        connection.queryForOne(isA(String.class), isA(Object[].class), isA(FieldType[].class),
            isA(GenericRowMapper.class), (ObjectCache) isNull())).andReturn(foreignForeign);
    connectionSource.releaseConnection(connection);
    DatabaseResults results = createMock(DatabaseResults.class);
    ForeignAutoRefresh foreign = new ForeignAutoRefresh();
    replay(results, connectionSource, connection);
    FieldType fieldType =
        FieldType.createFieldType(connectionSource, ForeignAutoRefresh.class.getSimpleName(), field,
View Full Code Here

public class TransactionManagerTest extends BaseCoreTest {

  @Test
  public void testTransactionManager() throws Exception {
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    expect(conn.isAutoCommitSupported()).andReturn(false);
    Savepoint savePoint = createMock(Savepoint.class);
    expect(savePoint.getSavepointName()).andReturn("name").anyTimes();
    expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint);
    conn.commit(savePoint);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    expect(connectionSource.saveSpecialConnection(conn)).andReturn(true);
    connectionSource.clearSpecialConnection(conn);
    connectionSource.releaseConnection(conn);
    replay(connectionSource, conn, savePoint);
    TransactionManager tm = new TransactionManager(connectionSource);
    tm.callInTransaction(new Callable<Void>() {
      public Void call() {
        return null;
View Full Code Here

    verify(connectionSource, conn, savePoint);
  }

  @Test
  public void testTransactionManagerSavePointNull() throws Exception {
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    expect(conn.isAutoCommitSupported()).andReturn(false);
    expect(conn.setSavePoint(isA(String.class))).andReturn(null);
    conn.commit(null);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    expect(connectionSource.saveSpecialConnection(conn)).andReturn(true);
    connectionSource.clearSpecialConnection(conn);
    connectionSource.releaseConnection(conn);
    replay(connectionSource, conn);
    TransactionManager tm = new TransactionManager(connectionSource);
    tm.callInTransaction(new Callable<Void>() {
      public Void call() {
        return null;
View Full Code Here

    verify(connectionSource, conn);
  }

  @Test
  public void testTransactionManagerRollback() throws Exception {
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    expect(conn.isAutoCommitSupported()).andReturn(false);
    Savepoint savePoint = createMock(Savepoint.class);
    expect(savePoint.getSavepointName()).andReturn("name").anyTimes();
    expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint);
    conn.rollback(savePoint);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    expect(connectionSource.saveSpecialConnection(conn)).andReturn(true);
    connectionSource.clearSpecialConnection(conn);
    connectionSource.releaseConnection(conn);
    replay(connectionSource, conn, savePoint);
    TransactionManager tm = new TransactionManager(connectionSource);
    try {
      tm.callInTransaction(new Callable<Void>() {
        public Void call() throws Exception {
View Full Code Here

TOP

Related Classes of com.j256.ormlite.support.ConnectionSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.