Simple Connect and SELECT Sample
From Ingres Community Wiki
Note: This code will connect to your local demodb database and retrieve all columns from table country. Database demodb is provided as sample database with Ingres 2006 Release 2 and later. It assumes your Instance name is II, if not please correct connection string to replace localhost:II7 with your instance name, such localhost:xx7 (where xx is your instance name).
import java.sql.*;
import java.io.*;
import com.ingres.jdbc.*;
public class demo01 {
public demo01() {
}
public static void main (String args[]) {
IngresDriver drv;
Connection conn;
String url="jdbc:ingres://localhost:II7/demodb;AUTO=multi";
String query = "select * from country";
System.out.println("Demo test: connecting ...");
try
{
drv = (IngresDriver)Class.forName("com.ingres.jdbc.IngresDriver").newInstance();
}
catch(java.lang.InstantiationException e)
{
System.err.print("InstantiationException: ");
System.err.println(e.getMessage());
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
catch(java.lang.IllegalAccessException e)
{
System.err.print("IllegalAccessException: ");
System.err.println(e.getMessage());
}
try
{
conn = DriverManager.getConnection(url);
if (conn == null)
System.out.println("Demo test: no driver available");
else
System.out.println("Demo test: conected at: " + conn);
}
catch (SQLException ex)
{
printSQLException(ex);
conn = null;
}
if (conn != null)
try
{
Statement stmt;
ResultSet rs;
int count=0;
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int rowCount = 1;
ResultSet rs1 = stmt.getResultSet();
System.out.println("Demo test: Retreiving Result Set");
System.out.println(" ");
while ( rs.next() )
{
System.out.println("Row " + rowCount + ": ");
for (int i = 1; i <= numberOfColumns; i++)
{
System.out.print(" Column " + i + ": ");
System.out.println(rs.getString(i));
}
System.out.println("");
rowCount++;
count++;
}
System.out.println("Demo test: done.");
System.out.println(" ");
System.out.println("Press ENTER key to exit...");
try {
int key = System.in.read();
} catch (java.io.IOException ioe) {
ioe.printStackTrace();
}
rs.close();
stmt.close();
conn.close();
}
catch( SQLException ex)
{
printSQLException(ex);
}
}
private static void
printSQLException( SQLException ex )
{
do
{
System.out.print( "SQLException: ' " );
System.out.print( ex.getSQLState() );
System.out.print( " ' 0x" );
System.out.print( Integer.toHexString( ex.getErrorCode() ) );
System.out.print( " -- " );
System.out.println( ex.getMessage() );
} while( (ex = ex.getNextException()) != null );
return;
}
}
