Fetch data with DBStorage

Contains useful source code for specific use cases.

Fetch data with DBStorage

Postby Support@SIB » Fri Jun 14, 2013 2:07 pm

Sometimes you want to use DBAccess and DBStorage without JVx server and life-cycle objects. This is very easy because JVx was designed as library. Simply use DBStorage and DBAccess in your Servlets, applications, ...

The following example uses DBAccess and DBStorage to check if a user exists in the database and if an email address doesn't exist.

Code: Select all
dba = DBAccess.getDBAccess("jdbc:oracle:thin:@localhost:1521:xe");
dba.setUsername("user");
dba.setPassword("password");
//also possible, but needs rad/apps/myapp/config.xml
//DBAccess dba = DBAccess.getDBAccess(DBSecurityManager.getCredentials(
//                             Configuration.getApplicationZone("myapp").getConfig()));
dba.open();
dba.getConnection().setAutoCommit(false);

DBStorage dbsUser = new DBStorage();
dbsUser.setDBAccess(dba);
dbsUser.setWritebackTable("USERS");
dbsUser.open();

//CHECK if the user exists
IBean bnUser = dbsUser.fetchBean(new Equals("USERNAME", pUserName));

if (bnUser != null)
{
    htResult.put("USER", "User already exists!");
}

//CHECK if the email exists
if (dbsUser.fetchBean(new LikeIgnoreCase("EMAIL", pEmail), null, 0, 1).size() > 0)
{
    htResult.put("EMAIL", "E-Mail already exists!");
}

If you prefer POJOs:

Code: Select all
User user = dbsUser.createPOJO(User.class, bnUser);
User avatar
Support@SIB
 
Posts: 198
Joined: Mon Sep 28, 2009 1:56 pm

Return to Code snippets