XXX
XXX
//
// JDBC with DB2 UDB Version 5.0 Demo Program
//
// Richard Sinn
//
import java.sql.*;
import java.util.*;
import java.io.*;
public class jdbctest
{
public static void main(String[] args)
{
String driver;
String url;
String username;
String password;
String select;
driver = "COM.ibm.db2.jdbc.app.DB2Driver";
url = "jdbc:db2:demodb";
username = "sinn";
password = "mypassword";
select = "";
//
// This could be changed to get input from GUI
//
//
// Ask user for SQL statement input
//
// Sample: select = "select TNUM, NAME, DEPT, DIV, USERID, NODE from SINN.EMP";
//
try
{
System.out.println("INFO: Please enter SQL statement");
select = (new DataInputStream(System.in)).readLine();
}
catch (java.lang.Exception ex)
{
System.err.println("ERROR: IO Failed");
ex.printStackTrace();
}
//
// Try to get the driver loaded
//
try
{
Class.forName(driver);
}
catch (java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
//
// Logic Main Line
//
try
{
Connection c;
c = DriverManager.getConnection(url, username, password);
Statement s = c.createStatement();
ResultSet rs = s.executeQuery(select);
ResultSetMetaData rsmd = rs.getMetaData();
//
// We can print the col types as well
//
// PrintColumnTypes.printColTypes(rsmd);
// System.out.println("");
//
// Print the content name
//
int numberOfColumns = rsmd.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++)
{
String columnName = rsmd.getColumnName(i);
int dSize = rsmd.getColumnDisplaySize(i);
int jdbcType = rsmd.getColumnType(i);
System.out.print(columnName);
// We assume that the display column size is bigger
// or equal to the length of column name, and we
// always print a space between the real field
for (int k = 1; k <= (dSize - columnName.length() + 1); k++)
{
// Print only one space for integer
if (jdbcType != 4)
{
System.out.print(" ");
}
else
{
System.out.print(" ");
break;
}
}
}
System.out.println("");
//
// Print the result content
//
while (rs.next())
{
for (int i = 1; i <= numberOfColumns; i++)
{
if (i > 1) System.out.print(" ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println("");
}
//
// Close our resource
//
s.close();
c.close();
}
catch (SQLException e)
{
System.err.println("ERROR: Invalid SQL Statement Entered");
System.err.println(e.getMessage());
e.printStackTrace();
return;
}
} // End of Main
} // End of Class File
XXX