/*
 * Kleines Tool zum Übertragen von Daten ´
 * zwischen beliebigen Datenbanken
 * erforderlich sind JDBC Treiber für
 * Quelldatenbank und Zieldatenbank
 * Die Übertragung wird durch properties gesteuert
 * Copyright Dieter Bender 05/2003
 */
package de.bender_dv.transfer;

import java.sql.*;
import java.util.*;

public class BasicDataTable 
{
	String propertyName;	
	String driverName;		
	String url;				
	String userName;
	String password;
	Connection con;
	ResourceBundle props;
	String select;
	int columnCount;
	Statement in;
	ResultSet rs;
	PreparedStatement out;
	String insert;

	public BasicDataTable()
	{
		super();
	}
	public BasicDataTable(String resourceName)
	{
		super();
		propertyName = resourceName; 
		getProperties();
		connect();
	}
	public Vector getRecord()
	{
		if(in == null)
		{
			createStmt();
		}
		if(rs == null)
		{
			select();
		}
		Vector result = new Vector();
		try
		{
			if (rs.next())
			{
				for(int i = 1; i <= columnCount;i++ )
				{
					result.addElement(rs.getObject(i));
				}
			}
			else
			{
				result = null;
				rs.close();
				rs = null;
			}	
		}
		catch(SQLException e)
		{
			System.err.println("getRecord " + e);
		}

		return result;
	}
	public boolean setRecord(Vector in)
	{
		boolean result = false;
		if(out == null)
		{
			prepare();
		}
		Object o = null;
		try
		{
			for(int i = 0; i < columnCount;i++ )
			{
				o = in.get(i);
				out.setObject(i +1 , o);
			}
			out.execute();
			result = true;
		}
		catch(SQLException e)
		{
			System.err.println("setRecord " + o + e);
		}		
		return result;
	}
	void createStmt()
	{
		try
		{
			in = con.createStatement();
		}
		catch(SQLException e)
		{
			System.err.println("e");
		}		
	}
	void prepare()
	{
		try
		{
			insert 	= props.getString("insert");
			out = con.prepareStatement(insert);
			System.out.println("prepared " + insert);
		}
		catch(Exception e)
		{
			System.err.println("e");
		}
	}
	void select()
	{
		try
		{
			select 	= props.getString("select");
			rs = in.executeQuery(select);
			System.out.println("executed " + select);
		}
		catch(Exception e)
		{
			System.err.println("e");
		}
	}
	void connect()
	{
		try 
		{
			Class.forName(driverName);
			con = DriverManager.getConnection(url, userName, password);
			System.out.println("Connected to " + url);
			con.setAutoCommit(true);
		} 
		catch(java.lang.Exception e) 
		{
			System.err.print("Connection : " + propertyName + " "); 
			System.err.println(e.getMessage());
		}
		
	}
	void close()
	{
		if(in != null)
		{
			try
			{
				in.close();
			}
			catch(SQLException e)
			{
				System.out.println("Schade : " + e);
			}			
		}
		if(out != null)
		{
			try
			{
				out.close();
			}
			catch(SQLException e)
			{
				System.out.println("Schade : " + e);
			}			
		}
		try
		{
			con.close();
		}
		catch(SQLException e)
		{
			System.out.println("Schade : " + e);
		}			
	}
	void getProperties()
	{
		try
		{
			props = ResourceBundle.getBundle(propertyName);
			driverName 	= props.getString("driver");
			url        	= props.getString("url");
			userName   	= props.getString("user");
			password   	= props.getString("password");
			columnCount = Integer.parseInt(props.getString("columnCount"));
		}
		catch(MissingResourceException e)
		{
			System.err.println(e);
		}
	}
}

