PostgreSQL with Java

11May10
package org.connect.jdbc;
import java.sql.*;

public class eksamenf {

	public static void main(String[] s) throws SQLException {
		String url, user, pass, ps;
		url = "jdbc:postgresql://erlend.*****.com/postgres";
		user = "postgres";
		pass = "*****";

		Connection db = DriverManager.getConnection(url, user, pass);
		
		TimerMinutter svar = finnTotalTid(10000,2009,db); 
		System.out.print("Total tid: " + svar.getTimer() + ":");
		System.out.println(svar.getMinutter());
		
	}
	
	public static TimerMinutter finnTotalTid(int ansattId, int aar, Connection c) throws SQLException {


		String sql ="select SUM(t.timer) as timer, SUM(t.minutter) as minutter " +
			"from timer t, ansatt a " +
			"where t.ansattid=a.id and a.id="+ansattId+" and " +
			"dato BETWEEN '01.01."+aar+"' and '31.12."+aar+"' " +
			"group by a.id;";
		
		PreparedStatement st = c.prepareStatement(sql);
		ResultSet r = st.executeQuery();
		

		TimerMinutter resultat = new TimerMinutter();
		
		while(r.next()) {
			resultat.setTimer(r.getInt("timer"));
			resultat.setMinutter(r.getInt("minutter"));
		}
		st.close();
	
	return resultat;		
	}
}





No Responses Yet to “PostgreSQL with Java”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: