import java.math.*;
import java.sql.*;

public class JdbcInterface {

	boolean    DEBUG = false;
	Connection conn = null;
	Statement  stmt = null;
    ResultSet  rs = null;
    ResultSetMetaData rsmd = null;
    
    // === Constructors/finalize  =========================================
    public JdbcInterface() {
    	this(true);
    } // end of default constructor

    public JdbcInterface(boolean b) {
    	DEBUG = b;
    } // end of constructor

    public int JdbcConnect(String[] parms) {
      int rc = 0;
      
      if (DEBUG)
      	System.out.println("In JdbcInterface: driver=" + parms[0]);
      
      try {
		if (DEBUG)
		  System.out.println("In try block");
			      
		if (parms[0] != null && !parms[0].isEmpty()) {
		  System.out.println("b is true!");
  		  Class.forName(parms[0]); //loads the driver
		} // endif driver
			
	    if (DEBUG)
		  System.out.println("URL=" + parms[1]);
	      
    	if (parms[2] != null && !parms[2].isEmpty()) {
  	      if (DEBUG)
  	      	System.out.println("user=" + parms[2] + " pwd=" + parms[3]);
  	      
    	  conn = DriverManager.getConnection(parms[1], parms[2], parms[3]);
    	} else
    	  conn = DriverManager.getConnection(parms[1]);

	    if (DEBUG)
		  System.out.println("Connection established");
	      
	    // Get a statement from the connection
	    stmt = conn.createStatement();

	    } catch(ClassNotFoundException e) {
	    	System.err.println("ClassNotFoundException: " + e.getMessage());
	    	rc = 1; 
		} catch (SQLException se) {
			System.out.println( "SQL Exception:" ) ;

			// Loop through the SQL Exceptions
			while (se != null) {
				System.out.println("State  : " + se.getSQLState());
			    System.out.println("Message: " + se.getMessage());
			    System.out.println("Error  : " + se.getErrorCode());

			    se = se.getNextException();
			} // end while se
			
	    	rc = 2; 
		} catch( Exception e ) {
			System.out.println(e);
	    	rc = 3; 
		} // end try/catch
      
      return rc;
    } // end of JdbcConnect

    public int JdbcDisconnect() {
      int rc = 0;
      
	  // Close the statement and the connection
	  if (rs != null)
		try {
		  rs.close();
		} catch(SQLException se) {
		  System.out.println(se);
		  rc = 1;
	    } // nothing more we can do
		      
	  if (stmt != null)
		try {
		  stmt.close();
		} catch(SQLException se) {
		  System.out.println(se);
		  rc += 2;
	    } // nothing more we can do
		      
      if (conn != null)
		try {
		  conn.close();
	    } catch (SQLException se) {
		  System.out.println(se);
		  rc += 4;
	    } //end try/catch
	
      return rc;
    } // end of JdbcDisconnect
    
    public int Execute(String query) {
	      int n = 0;
	      
	      if (DEBUG)
			System.out.println("Executing '" + query + "'");
	    	
	      try {
	    	boolean b = stmt.execute(query);
	    	
	    	if (b == false) {
	    		n = stmt.getUpdateCount();
	    		if (rs != null) rs.close();
	    	} // endif b
	    	
	    	if (DEBUG)
			  System.out.println("Query '" + query + "' executed successfully");
	    		
	      } catch (SQLException se) {
			System.out.println(se);
	      } //end try/catch

	      return n;
	    } // end of Execute
    
    public int GetResult() {
    	int ncol = 0;
    	
    	try {
    		rs = stmt.getResultSet();
    		
    		if (rs != null) {
			  rsmd = rs.getMetaData();
	    	  ncol = rsmd.getColumnCount();
	    	
	    	  if (DEBUG)
			    System.out.println("Result set has " + rsmd.getColumnCount() + " column(s)");
	    	
    		} // endif rs
	    		
	      } catch (SQLException se) {
			System.out.println(se);
	      } //end try/catch

	    return ncol;
    } // end of GetResult
	    
    public int ExecuteQuery(String query) {
      int ncol = 0;
      
      if (DEBUG)
		System.out.println("Executing '" + query + "'");
    	
      try {
    	rs = stmt.executeQuery(query);
		rsmd = rs.getMetaData();
    	ncol = rsmd.getColumnCount();
    	
    	if (DEBUG) {
		  System.out.println("Query '" + query + "' executed successfully");
		  System.out.println("Result set has " + rsmd.getColumnCount() + " column(s)");
    	} // endif DEBUG
    		
      } catch (SQLException se) {
		System.out.println(se);
      } //end try/catch

      return ncol;
    } // end of ExecuteQuery
    
    public boolean ReadNext() {
	  if (rs != null) {
	    try {
	  	  return rs.next();
		} catch (SQLException se) {
		  System.out.println(se);
		} //end try/catch
	    	  
	  } // endif rs
	      
      return false;
	} // end of ReadNext
	      
    public String ColumnName(int n) {
      if (rsmd == null) {
		System.out.println("No result metadata");
      } else try {
    	return rsmd.getColumnName(n);
      } catch (SQLException se) {
		System.out.println(se);
      } //end try/catch
    	  
      return null;  
    } // end of ColumnName
    
    public int ColumnType(int n) {
	  if (rsmd == null) {
		System.out.println("No result metadata");
	  } else try {
	    return rsmd.getColumnType(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return 0;  
	} // end of ColumnType
	    
    public String StringField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getString(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return null;  
	} // end of StringField
	    
    public int IntField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getInt(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return 0;  
	} // end of IntField
	    
    public long BigintField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
		BigDecimal bigDecimal = rs.getBigDecimal(n);
        return bigDecimal != null ? bigDecimal.longValue() : null;
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return 0;  
	} // end of BiginttField
	    
    public double DoubleField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getDouble(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return 0.;  
	} // end of DoubleField
	    
    public float FloatField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getFloat(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return 0;  
	} // end of FloatField
	    
    public boolean BooleanField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getBoolean(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return false;  
	} // end of BooleanField
	    
    public Date DateField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getDate(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return null;  
	} // end of DateField
	    
    public Time TimeField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getTime(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return null;  
	} // end of TimeField
	    
    public Timestamp TimestampField(int n) {
	  if (rs == null) {
		System.out.println("No result set");
	  } else try {
	    return rs.getTimestamp(n);
	  } catch (SQLException se) {
		System.out.println(se);
	  } //end try/catch
	    	  
	  return null;  
	} // end of TimestampField
	    
} // end of class JdbcInterface
