Archive for March, 2010

Creating a portable subversion repository

March 15, 2010 Leave a comment

I generally move between different systems when developing code. An easy way to keep things in sync and transfer my code was to use a flash drive as a subversion repository and use the flash drive between the systems.
Creating a repository was dead simple. Run this command in the flash drive dir in which you want the repository..
svnadmin –pre-1.5-compatible create repo-name
Replace the repo-name with an appropriate name.
To checkout the code use the following (in Ubuntu)
svn co file:///media/usb/code/repo-name
Note: The flash drive may be mounted at a different location than the one mentioned above(/media/usb)

Categories: Subversion, Ubuntu Tags: ,

Accessing databases using datasource xml

March 15, 2010 Leave a comment

We will use the previous struts project to setup database access to MySQL running on localhost.
Run the following command to create the .project and .classpath files for Eclipse and import the project in Eclipse

mvn eclipse:m2eclipse

Download the database driver mysql-connector-java-5.1.11-bin.jar from here and save it to the lib folder in this location $JBOSS_HOME/server/web/lib

Create a jboss-web.xml file in the WEB-INF folder with the following content

<?xml version="1.0" encoding="UTF-8"?>

In the web.xml file add the following :


Create a datasource xml file and add it to the deploy dir of JBoss.

Here is the content of my file:

 <!-- Typemapping for JBoss 4.0 -->

Replace database-name, username and password with appropriate values.

To keep things simple to test, we will add an action in struts-config and have the Action class call the database.

In the action class add this

private final String DATABASE_JNDI_SOURCE = "java:CodesiloDS";

and have this in the execute method. (In the real world most of this code would reside in your model and any helper classes.)

public ActionForward execute(ActionMapping mapping, ActionForm form,
 HttpServletRequest request, HttpServletResponse response)
 throws Exception {

 Connection conn = null;
 Statement stmt = null;
 ResultSet rs = null;
 String date ="";
 Context ctx = new InitialContext();
 DataSource ds = (DataSource)ctx.lookup(DATABASE_JNDI_SOURCE);
 if(ds!= null){
 conn = ds.getConnection();
 stmt = conn.createStatement();
 rs = stmt.executeQuery("select curdate() from dual;");
 date = rs.getString(1);
 catch(Exception e){
 ---- code here----
 catch(Exception e){
 ---- code here -----
 System.out.println("This is a test: " + date);
 return mapping.findForward("success");

Once we call the action, we should see the current date printed on the console.