edu.upc.dama.dex.io
Class JDBCReader

java.lang.Object
  extended by edu.upc.dama.dex.io.JDBCReader
All Implemented Interfaces:
RowReader

public class JDBCReader
extends java.lang.Object
implements RowReader

A very simple JDBC reader.

This reader allows for getting rows from a relational database using the JDBC interface.
To use it you must get the JDBC driver of your database.
After this, you can execute a query over the database and get the result row by row.

 Basic example using a MySQL database:
RowReader rl = new JDBCReader("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/example", "SELECT * FROM `test` "); String[] r; while ((r = rl.readNext()) != null) { for (int i = 0; i < r.length; i++) { System.out.println(r[i]); } }

Author:
Sparsity Technologies

Constructor Summary
JDBCReader(java.lang.String driverUrl, java.lang.String url, java.lang.String query)
          Creates a new instance of JDBCReader.
JDBCReader(java.lang.String driverUrl, java.lang.String protocol, java.lang.String database, java.lang.String query)
          Creates a new instance of JDBCReader.
JDBCReader(java.lang.String driverUrl, java.lang.String url, java.lang.String user, java.lang.String password, java.lang.String query)
          Creates a new instance of JDBCReader.
 
Method Summary
 void close()
          Closes the underlying reader.
 int getRow()
          The row number for the current row.
 java.lang.String[] readNext()
          Reads the next row as a string array.
 boolean reset()
          Moves the reader to the first row of the result of the query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JDBCReader

public JDBCReader(java.lang.String driverUrl,
                  java.lang.String protocol,
                  java.lang.String database,
                  java.lang.String query)
           throws java.io.IOException
Creates a new instance of JDBCReader.

Parameters:
driverUrl - JDBC driver url .
protocol - Protocol to be used for the JDBC connection.
database - Name of the database.
query - Query to be executed on the database.
Throws:
java.io.IOException

JDBCReader

public JDBCReader(java.lang.String driverUrl,
                  java.lang.String url,
                  java.lang.String query)
           throws java.io.IOException
Creates a new instance of JDBCReader.

Parameters:
driverUrl - JDBC driver url
url - JDBC url connection to the database.
query - Query to be executed on the database.
Throws:
java.io.IOException

JDBCReader

public JDBCReader(java.lang.String driverUrl,
                  java.lang.String url,
                  java.lang.String user,
                  java.lang.String password,
                  java.lang.String query)
           throws java.io.IOException
Creates a new instance of JDBCReader.

Parameters:
driverUrl - JDBC driver url
url - JDBC url connection to the database.
user - Username of the database.
password - Password of to the database.
query - Query to be executed on the database.
Throws:
java.io.IOException
Method Detail

reset

public boolean reset()
              throws java.io.IOException
Moves the reader to the first row of the result of the query.

Restarts the reader.

Specified by:
reset in interface RowReader
Returns:
true.
Throws:
java.io.IOException - If bad things happen.

readNext

public java.lang.String[] readNext()
                            throws java.io.IOException
Reads the next row as a string array.

Specified by:
readNext in interface RowReader
Returns:
A string array with each comma-separated element as a separate entry.
Throws:
java.io.IOException - If bad things happen during the read.

close

public void close()
           throws java.io.IOException
Closes the underlying reader.

Specified by:
close in interface RowReader
Throws:
java.io.IOException - If the close fails.

getRow

public int getRow()
           throws java.io.IOException
The row number for the current row.

Specified by:
getRow in interface RowReader
Returns:
The current row number; 0 if there is no current row.
Throws:
java.io.IOException - If it fails.