Error getting connection to the database

hello vaadiners

I am having problems connecting to my db, and I am sure the problem is on the java side

I use a static class to connect to the DB as you can see there is 2 approach to do this

  • simple jdbc
  • and the said to be more advanced/effective datasource

if I use the good old jdbc approach everything is fine , but when I try to use the datasource approach after a while everything goes wrong: I always get the loading indicator at the login srceen ( my very first attempt to connect to the db)

I am using:

  • tomcat7
  • postgresql 9 db server
  • the latest postgresql jdbc4 driver (postgresql-9.1-901.jdbc4.jar)
  • jdk/jre 6

here is my code for the db class:


package com.digicpictures.dproject.data;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public  class DB {
	
	/*csatlakozás az adatbázishoz DATASOURCE-val*/
	
//	static DataSource ds = null;
//	static {
//
//		Context ctx = null;
//		try {
//			ctx = (Context) new InitialContext().lookup("java:comp/env");
//		}
//		catch (NamingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		try {
//			ds = (DataSource) ctx.lookup("jdbc/dproject");
//			System.out.println("APPLICATION DATASOURCE INITIALIZED ");
//			
//			
//		} 
//		catch (NamingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		
//		
//		
//		
//		
//	}
	
/*csatlakozás az adatbázishoz SIMA JDBC-vel*/	
	
	private static final Properties properties = new Properties();
	private static final String JDBCdriver;
	private static final String JDBCpassword; 
	private static final String JDBCuser;
	private static final String JDBCurl;
	private static final String JDBCdatabase;
	private static final String JDBChost;
	private static final String JDBCport;
	private static final String JDBCparameters;
	
	
static {
		 
     //db config file betöltése
     InputStream is = DB.class.getResourceAsStream("/database.properties");
     try {
		properties.load(is);
		System.out.println("DB config file loaded");
	} catch (IOException e1) {
		System.out.println("ERROR: unable to load DB config file");
	}

    JDBCdriver = properties.getProperty("driver");
	JDBCpassword=properties.getProperty("password"); 
	JDBCuser=properties.getProperty("username");
	JDBCdatabase=properties.getProperty("database");
	JDBChost=properties.getProperty("host");
	JDBCport=properties.getProperty("port");
	JDBCparameters=properties.getProperty("connection-parameters");
	
	
	JDBCurl = "jdbc:postgresql://"+JDBChost+":"+JDBCport+"/"+JDBCdatabase+"?"+JDBCparameters;
	
	
	
	//jdbc driver betöltése
	try {
		Class.forName(JDBCdriver);
		System.out.println("JDBC driver loaded SUCCESSFULLY");
		System.out.println("Connected to <"+JDBCdatabase+"> database");
		System.out.println("Connection parameters: "+JDBCparameters);
	}
	
	catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		System.out.println(" ERROR: JDBC driver could not be loaded/registered");
	}

	
}
public static Connection getConnection() throws SQLException {
//	return ds.getConnection();
	return DriverManager.getConnection(JDBCurl, JDBCuser, JDBCpassword);
}

}

and here is a typical code for query-ing the database:


String sql = "SELECT\r\n" + "  dpt_tag.dpc_handle,  \r\n"
				+ "  dpt_tag.dpc_name \r\n" + "  \r\n" + "FROM \r\n"
				+ "  public.dpt_tag\r\n" + "ORDER BY\r\n"
				+ "  dpt_tag.dpc_name ASC";

		try {
			java.sql.Connection conn = DB.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);

			while (rs.next()) {
				Item item = tokenContainer.addItem(rs.getString("dpc_handle"));
				item.getItemProperty("dpc_name").setValue(
						rs.getString("dpc_name"));
			}
			stmt.close();
			rs.close();
			conn.close();
		}

		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

In the future I would like to use the datasource approach but I havent been able to find out what is wrong with this approach

What can be wrong with my code?

Is there a better way to connect to the db?

I can provide more details upon request

please help me to solve this mistery

Peter