473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Tabelspace Informations

Hi,
is there a way to get informations from tabelspaces of a db2
(datafiles) via java (jdbc)?

Thanx...
Stefan

Jun 1 '06 #1
4 1383
Stefan,
snapshot_tbs & snapshot_tbs_cfg are the table function u might want to
look at

For more information
http://publib.boulder.ibm.com/infoce...n/r0007101.htm

Stefan wrote:
Hi,
is there a way to get informations from tabelspaces of a db2
(datafiles) via java (jdbc)?

Thanx...
Stefan


Jun 1 '06 #2

"Stefan" <in**@daten-host.de> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi,
is there a way to get informations from tabelspaces of a db2
(datafiles) via java (jdbc)?

Quite a lot of information about DB2 is available via Java/JDBC but I'm not
sure if tablespace information is available. What specific information do
you want Java/JDBC to get for you?

Also, what version of DB2 and what operating system are you using?
--
Rhino
Jun 1 '06 #3

"Peri" <pe*******@gmail.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
Stefan,
snapshot_tbs & snapshot_tbs_cfg are the table function u might want to
look at

For more information
http://publib.boulder.ibm.com/infoce...n/r0007101.htm

Assuming you are using DB2 for Windows/Linux/Unix version 8, the following
short program illustrates how you could get the information from the
snapshot_tbs table function via a Java program:

---------------------------------------------------------------------------------------------------------------------
package com.foo.db2v8;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @version 1.0
*
* <p>
* Demonstrate use of DB2 table functions for getting information via
Java/JDBC.
* </p>
*
* <p>
* This program:
* <ul>
* <li>loads the JDBC Driver used to access DB2. In order for the driver to
be
* loaded successfully, the appropriate zip or jar file containing the
driver
* needs to be visible during the compile/execution of this program. The
files containing
* the JDBC drivers are in the sqllib\java directory.
* <li>connects to the DB2 Sample database
* <li>queries the database via a table function and displays the results of
the function
* </ul>
* </p>
*
* <p>
* All results are displayed on the Java console to simplify the code; a
real
* program would more likely display information in a GUI (Graphical User
* Interface). When needed, input is obtained from the command line; a real
* program would more likely obtain input from a GUI.
* </p>
*
* <p>
* All of the SQL statements executed in this program used
java.sql.Statement
* objects. In other words, none of them use parameter markers ('?' symbol)
to
* indicate variables; all variable values are hard-coded right in the
program.
* </p>
*
* <p>
* This program assumes that no nulls are sent or returned by any SQL
statement.
* </p>
*
* <p>
* Error handling is used throughout the program to ensure that the program
* terminates with a meaningful message if something didn't work correctly.
The
* following try/catch block is very useful in obtaining diagnostic
information
* from JDBC, which gets it from your database:
* </p>
* <xmp>
* try {
* //your SQL statement
* }
* catch (SQLException sql_excp) {
* System.err.println("SQLState: " + sql_excp.getSQLState());
* System.err.println("SQLCode: " + sql_excp.getErrorCode());
* System.err.println("Message: " + sql_excp.getMessage());
* }
* </xmp>
*
* <p>
* A single connection and a single thread is used for the entire life of
this
* program. In a multi-user environment, this program might use a connection
* pool to ensure sufficient users could run this program simultaneously. If
the
* program had to do several things in parallel, additional threads could be
* used.
* </p>
*
* <p>
* This program leaves "autocommit" on so that all commits are done
automatically. Other
* approaches are possible and may be preferable, depending on your
circumstances.
* </p>
*
* <p>
* Variables that are only used in one method of this application are
defined as
* method variables; all other variables are defined as class variables.
Other
* approaches are possible and may be preferable, depending on your
* circumstances.
* </p>
*/
public class JDBC04 {

/*
*
* Class variables.
*
*/
/** The name of this class. */
final String CLASS_NAME = getClass().getName();

/*
*
* Instance variables.
*
*/
/** The database connection used by this program. */
Connection conn01 = null;

/**
* main method for this application.
*/
public static void main(String[] args) {

/* Validate the command-line arguments, if any. */
if (args.length != 0) {
System.err.println("This program should not receive any
command-line arguments but it received these " + args.length + " arguments:
"); //$NON-NLS-1$ //$NON-NLS-2$
for (String oneArg : args) {
System.err.println(" " + oneArg); //$NON-NLS-1$
}
System.err.println("These arguments were ignored.");
//$NON-NLS-1$
}

new JDBC04();
}

/**
* Constructor for this class.
*/
public JDBC04() {

System.out.println("Welcome to " + this.CLASS_NAME + "!\n");
//$NON-NLS-1$ //$NON-NLS-2$

loadDriver();
connectToDatabase();
queryTable();
closeConnection();
}

/**
* Load the JDBC driver.
*/
public void loadDriver() {

String METHOD_NAME = "loadDriver()"; //$NON-NLS-1$

/* Initialize the variable that contains the name of the JDBC
driver. */
String jdbcDriverName = "com.ibm.db2.jcc.DB2Driver"; //$NON-NLS-1$

/* Load the JDBC driver. */
try {
Class.forName(jdbcDriverName);
} catch (ClassNotFoundException cnf_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered ClassNotFoundException while attempting to load JDBC driver " +
jdbcDriverName + ". Message: " + cnf_excp.getMessage()); //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$
cnf_excp.printStackTrace();
System.exit(16);
}
}

/**
* Get a connection to the database.
*
*/
public void connectToDatabase() {

String METHOD_NAME = "connectToDatabase()"; //$NON-NLS-1$

/* Initialize the variables used to get the connection. */
String databaseName = "sample"; //$NON-NLS-1$
String url = "jdbc:db2:" + databaseName; //$NON-NLS-1$
String loginName = "db2admin"; //$NON-NLS-1$
String password = "db2admin"; //$NON-NLS-1$

/* Connect to the database. */
try {
this.conn01 = DriverManager.getConnection(url, loginName,
password);
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException on connect to URL " + url + ". Message: " +
sql_excp.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
sql_excp.printStackTrace();
System.exit(16);
}

/*
* Obtain JDBC version. NOTE: Only available in Java 1.4 and above.
*/
try {
DatabaseMetaData dbMeta = this.conn01.getMetaData();
System.out.println("JDBC Version: " +
dbMeta.getJDBCMajorVersion() + "." + dbMeta.getJDBCMinorVersion());
//$NON-NLS-1$ //$NON-NLS-2$
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException on attempt to turn autocommit off. Message: " +
sql_excp.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}
}

/**
* Display the tablespace information.
*/
public void queryTable() {

String METHOD_NAME = "queryTable()"; //$NON-NLS-1$

System.out.println("\nDisplay tablespace information: ");
//$NON-NLS-1$
String queryTableSQL = "select * from
table(snapshot_tbs('SAMPLE', -2)) as tf"; //$NON-NLS-1$

/*
* Query the demonstration table to get information about certain
* employees.
*/
Statement queryTableStmt = null;
ResultSet rs01 = null;
try {
queryTableStmt = this.conn01.createStatement();
rs01 = queryTableStmt.executeQuery(queryTableSQL);
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while trying to get information about tablespaces.
Message: " + sql_excp); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}

/*
* Print a title line above the result set. The static method pad()
is
* used to align the column titles and underlines.
*/
String spaces = " "; //$NON-NLS-1$
System.out.println(
pad("SNAPSHOT_TIMESTAMP", ' ', 'T', 26) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_L_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_P_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_DATA_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_DATA_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_L_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_P_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_INDEX_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_READ_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_WRITE_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_READ_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_WRITE_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_DATA_READ_REQS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_READS", ' ', 'T', 25) + spaces + //$NON-NLS-1$
pad("DIRECT_WRITES", ' ', 'T', 25) + spaces + //$NON-NLS-1$
pad("DIRECT_READ_REQS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_WRITE_REQS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_READ_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_WRITE_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("UNREAD_PREFETCH_PAGES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_INDEX_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_TO_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_TO_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_FROM_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_FROM_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("FILES_CLOSED", ' ', 'T', 25) + spaces + //$NON-NLS-1$
pad("TABLESPACE_NAME", ' ', 'T', 128) //$NON-NLS-1$
);
System.out.println(
pad("--------------------------", ' ', 'T', 26) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("---------------", ' ', 'T', 128) //$NON-NLS-1$
);

/*
* Print each line of the result set.
*/
try {
while (rs01.next()) {
System.out.println(
pad(rs01.getTimestamp("SNAPSHOT_TIMESTAMP").toStri ng(),
' ', 'T', 26) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_L_READS" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_P_READS" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_DATA_RE ADS")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_WRITES") ),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_DATA_WR ITES")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_L_READS ")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_P_READS ")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_WRITES" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_INDEX_W RITES")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_READ_TIME")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_WRITE_TIME")) ,
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_READ_TI ME")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_WRITE_T IME")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_DATA_RE AD_REQS")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_READS")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_WRITES")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_READ_REQS") ),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_WRITE_REQS" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_READ_TIME") ),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_WRITE_TIME" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("UNREAD_PREFETCH_PA GES")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_INDEX_R EADS")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_TO_ESTOR E")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_TO_ESTO RE")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_FROM_ES TORE")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_FROM_EST ORE")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("FILES_CLOSED")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(rs01.getString("TABLESPACE_NAME"), ' ', 'T',
128) //$NON-NLS-1$
);
}
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while reading tablespace information. Message: " +
sql_excp); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}

/* Close the result set and dispose of the statement. */
try {
rs01.close();
queryTableStmt.close();
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while closing result set or closing statement.
Message: " + sql_excp); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}
}

/**
* Close the database connection.
*/
public void closeConnection() {

String METHOD_NAME = "closeConnection()"; //$NON-NLS-1$

/* Close the database connection. */
try {
this.conn01.close();
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while closing database connection. Message: " +
sql_excp.getMessage()); //$NON-NLS-1$//$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}
}

/**
* Pad an input string with either leading or trailing occurrences of a
* given character until the string reaches a given length.
*
* @param input is the input String which is to be padded.
* @param padChar is the character which is to be used to pad the input
String.
* @param padPosition is the location of the padding, either leading or
trailing.
* @param finalLength is the final length of the padded String.
* @return the padded String.
*/
public static String pad(String input, char padChar, char padPosition,
int finalLength) {

String METHOD_NAME = "pad()"; //$NON-NLS-1$

/*
* Pad position must be 'l' or 'L' for "leading" or 't' or 'T' for
"trailing".
*/
if (padPosition != 'L' & padPosition != 'l' & padPosition != 'T' &
padPosition != 't') {
throw new IllegalArgumentException(METHOD_NAME + " - The pad
position requested, " + padPosition + ", is not equal to L, l, T, or t.");
//$NON-NLS-1$ //$NON-NLS-2$
}

/*
* Final length must be greater than or equal to the length of the
input string.
*/
if (finalLength < input.length()) {
throw new IllegalArgumentException(METHOD_NAME + " - The final
length requested, " + finalLength + ", is less than the length, " +
input.length() + ", of the input string, " + input + "."); //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}

/*
* Determine the number of occurrences of the pad character that
will
* have to be added to the input string.
*/
int numPadChars = finalLength - input.length();

/*
* If the pad position is 'L' or 'l' for "leading", write the pad
* characters followed by the input string. Otherwise, write the pad
* characters after the input string.
*/
StringBuffer strbuffPaddedString = new StringBuffer();
if (padPosition == 'L' | padPosition == 'l') {
for (int ix = 0; ix < numPadChars; ix++)
strbuffPaddedString.append(padChar);
strbuffPaddedString.append(input);
} else {
strbuffPaddedString.append(input);
for (int ix = 0; ix < numPadChars; ix++)
strbuffPaddedString.append(padChar);
}

return (strbuffPaddedString.toString());
}
}---------------------------------------------------------------------------------------------------------------------
Stefan wrote:
Hi,
is there a way to get informations from tabelspaces of a db2
(datafiles) via java (jdbc)?

Thanx...
Stefan

Jun 1 '06 #4

"Peri" <pe*******@gmail.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
Stefan,
snapshot_tbs & snapshot_tbs_cfg are the table function u might want to
look at

For more information
http://publib.boulder.ibm.com/infoce...n/r0007101.htm

Assuming you are using DB2 for Windows/Linux/Unix version 8, the following
short program illustrates how you could get the information from the
snapshot_tbs table function via a Java program:

---------------------------------------------------------------------------------------------------------------------
package com.foo.db2v8;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @version 1.0
*
* <p>
* Demonstrate use of DB2 table functions for getting information via
Java/JDBC.
* </p>
*
* <p>
* This program:
* <ul>
* <li>loads the JDBC Driver used to access DB2. In order for the driver to
be
* loaded successfully, the appropriate zip or jar file containing the
driver
* needs to be visible during the compile/execution of this program. The
files containing
* the JDBC drivers are in the sqllib\java directory.
* <li>connects to the DB2 Sample database
* <li>queries the database via a table function and displays the results of
the function
* </ul>
* </p>
*
* <p>
* All results are displayed on the Java console to simplify the code; a
real
* program would more likely display information in a GUI (Graphical User
* Interface). When needed, input is obtained from the command line; a real
* program would more likely obtain input from a GUI.
* </p>
*
* <p>
* All of the SQL statements executed in this program used
java.sql.Statement
* objects. In other words, none of them use parameter markers ('?' symbol)
to
* indicate variables; all variable values are hard-coded right in the
program.
* </p>
*
* <p>
* This program assumes that no nulls are sent or returned by any SQL
statement.
* </p>
*
* <p>
* Error handling is used throughout the program to ensure that the program
* terminates with a meaningful message if something didn't work correctly.
The
* following try/catch block is very useful in obtaining diagnostic
information
* from JDBC, which gets it from your database:
* </p>
* <xmp>
* try {
* //your SQL statement
* }
* catch (SQLException sql_excp) {
* System.err.println("SQLState: " + sql_excp.getSQLState());
* System.err.println("SQLCode: " + sql_excp.getErrorCode());
* System.err.println("Message: " + sql_excp.getMessage());
* }
* </xmp>
*
* <p>
* A single connection and a single thread is used for the entire life of
this
* program. In a multi-user environment, this program might use a connection
* pool to ensure sufficient users could run this program simultaneously. If
the
* program had to do several things in parallel, additional threads could be
* used.
* </p>
*
* <p>
* This program leaves "autocommit" on so that all commits are done
automatically. Other
* approaches are possible and may be preferable, depending on your
circumstances.
* </p>
*
* <p>
* Variables that are only used in one method of this application are
defined as
* method variables; all other variables are defined as class variables.
Other
* approaches are possible and may be preferable, depending on your
* circumstances.
* </p>
*/
public class JDBC04 {

/*
*
* Class variables.
*
*/
/** The name of this class. */
final String CLASS_NAME = getClass().getName();

/*
*
* Instance variables.
*
*/
/** The database connection used by this program. */
Connection conn01 = null;

/**
* main method for this application.
*/
public static void main(String[] args) {

/* Validate the command-line arguments, if any. */
if (args.length != 0) {
System.err.println("This program should not receive any
command-line arguments but it received these " + args.length + " arguments:
"); //$NON-NLS-1$ //$NON-NLS-2$
for (String oneArg : args) {
System.err.println(" " + oneArg); //$NON-NLS-1$
}
System.err.println("These arguments were ignored.");
//$NON-NLS-1$
}

new JDBC04();
}

/**
* Constructor for this class.
*/
public JDBC04() {

System.out.println("Welcome to " + this.CLASS_NAME + "!\n");
//$NON-NLS-1$ //$NON-NLS-2$

loadDriver();
connectToDatabase();
queryTable();
closeConnection();
}

/**
* Load the JDBC driver.
*/
public void loadDriver() {

String METHOD_NAME = "loadDriver()"; //$NON-NLS-1$

/* Initialize the variable that contains the name of the JDBC
driver. */
String jdbcDriverName = "com.ibm.db2.jcc.DB2Driver"; //$NON-NLS-1$

/* Load the JDBC driver. */
try {
Class.forName(jdbcDriverName);
} catch (ClassNotFoundException cnf_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered ClassNotFoundException while attempting to load JDBC driver " +
jdbcDriverName + ". Message: " + cnf_excp.getMessage()); //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$
cnf_excp.printStackTrace();
System.exit(16);
}
}

/**
* Get a connection to the database.
*
*/
public void connectToDatabase() {

String METHOD_NAME = "connectToDatabase()"; //$NON-NLS-1$

/* Initialize the variables used to get the connection. */
String databaseName = "sample"; //$NON-NLS-1$
String url = "jdbc:db2:" + databaseName; //$NON-NLS-1$
String loginName = "db2admin"; //$NON-NLS-1$
String password = "db2admin"; //$NON-NLS-1$

/* Connect to the database. */
try {
this.conn01 = DriverManager.getConnection(url, loginName,
password);
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException on connect to URL " + url + ". Message: " +
sql_excp.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
sql_excp.printStackTrace();
System.exit(16);
}

/*
* Obtain JDBC version. NOTE: Only available in Java 1.4 and above.
*/
try {
DatabaseMetaData dbMeta = this.conn01.getMetaData();
System.out.println("JDBC Version: " +
dbMeta.getJDBCMajorVersion() + "." + dbMeta.getJDBCMinorVersion());
//$NON-NLS-1$ //$NON-NLS-2$
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException on attempt to turn autocommit off. Message: " +
sql_excp.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}
}

/**
* Display the tablespace information.
*/
public void queryTable() {

String METHOD_NAME = "queryTable()"; //$NON-NLS-1$

System.out.println("\nDisplay tablespace information: ");
//$NON-NLS-1$
String queryTableSQL = "select * from
table(snapshot_tbs('SAMPLE', -2)) as tf"; //$NON-NLS-1$

/*
* Query the demonstration table to get information about certain
* employees.
*/
Statement queryTableStmt = null;
ResultSet rs01 = null;
try {
queryTableStmt = this.conn01.createStatement();
rs01 = queryTableStmt.executeQuery(queryTableSQL);
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while trying to get information about tablespaces.
Message: " + sql_excp); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}

/*
* Print a title line above the result set. The static method pad()
is
* used to align the column titles and underlines.
*/
String spaces = " "; //$NON-NLS-1$
System.out.println(
pad("SNAPSHOT_TIMESTAMP", ' ', 'T', 26) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_L_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_P_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_DATA_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_DATA_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_L_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_P_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_INDEX_WRITES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_READ_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_WRITE_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_READ_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_WRITE_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_DATA_READ_REQS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_READS", ' ', 'T', 25) + spaces + //$NON-NLS-1$
pad("DIRECT_WRITES", ' ', 'T', 25) + spaces + //$NON-NLS-1$
pad("DIRECT_READ_REQS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_WRITE_REQS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_READ_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("DIRECT_WRITE_TIME", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("UNREAD_PREFETCH_PAGES", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_ASYNC_INDEX_READS", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_TO_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_TO_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_INDEX_FROM_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("POOL_DATA_FROM_ESTORE", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("FILES_CLOSED", ' ', 'T', 25) + spaces + //$NON-NLS-1$
pad("TABLESPACE_NAME", ' ', 'T', 128) //$NON-NLS-1$
);
System.out.println(
pad("--------------------------", ' ', 'T', 26) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("-------------------------", ' ', 'T', 25) + spaces +
//$NON-NLS-1$
pad("---------------", ' ', 'T', 128) //$NON-NLS-1$
);

/*
* Print each line of the result set.
*/
try {
while (rs01.next()) {
System.out.println(
pad(rs01.getTimestamp("SNAPSHOT_TIMESTAMP").toStri ng(),
' ', 'T', 26) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_L_READS" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_P_READS" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_DATA_RE ADS")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_WRITES") ),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_DATA_WR ITES")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_L_READS ")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_P_READS ")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_WRITES" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_INDEX_W RITES")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_READ_TIME")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_WRITE_TIME")) ,
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_READ_TI ME")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_WRITE_T IME")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_DATA_RE AD_REQS")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_READS")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_WRITES")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_READ_REQS") ),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_WRITE_REQS" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_READ_TIME") ),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("DIRECT_WRITE_TIME" )),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("UNREAD_PREFETCH_PA GES")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_ASYNC_INDEX_R EADS")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_TO_ESTOR E")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_TO_ESTO RE")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_INDEX_FROM_ES TORE")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("POOL_DATA_FROM_EST ORE")),
' ', 'L', 25) + spaces + //$NON-NLS-1$
pad(Long.toString(rs01.getLong("FILES_CLOSED")), '
', 'L', 25) + spaces + //$NON-NLS-1$
pad(rs01.getString("TABLESPACE_NAME"), ' ', 'T',
128) //$NON-NLS-1$
);
}
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while reading tablespace information. Message: " +
sql_excp); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}

/* Close the result set and dispose of the statement. */
try {
rs01.close();
queryTableStmt.close();
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while closing result set or closing statement.
Message: " + sql_excp); //$NON-NLS-1$ //$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}
}

/**
* Close the database connection.
*/
public void closeConnection() {

String METHOD_NAME = "closeConnection()"; //$NON-NLS-1$

/* Close the database connection. */
try {
this.conn01.close();
} catch (SQLException sql_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Encountered SQLException while closing database connection. Message: " +
sql_excp.getMessage()); //$NON-NLS-1$//$NON-NLS-2$
sql_excp.printStackTrace();
System.exit(16);
}
}

/**
* Pad an input string with either leading or trailing occurrences of a
* given character until the string reaches a given length.
*
* @param input is the input String which is to be padded.
* @param padChar is the character which is to be used to pad the input
String.
* @param padPosition is the location of the padding, either leading or
trailing.
* @param finalLength is the final length of the padded String.
* @return the padded String.
*/
public static String pad(String input, char padChar, char padPosition,
int finalLength) {

String METHOD_NAME = "pad()"; //$NON-NLS-1$

/*
* Pad position must be 'l' or 'L' for "leading" or 't' or 'T' for
"trailing".
*/
if (padPosition != 'L' & padPosition != 'l' & padPosition != 'T' &
padPosition != 't') {
throw new IllegalArgumentException(METHOD_NAME + " - The pad
position requested, " + padPosition + ", is not equal to L, l, T, or t.");
//$NON-NLS-1$ //$NON-NLS-2$
}

/*
* Final length must be greater than or equal to the length of the
input string.
*/
if (finalLength < input.length()) {
throw new IllegalArgumentException(METHOD_NAME + " - The final
length requested, " + finalLength + ", is less than the length, " +
input.length() + ", of the input string, " + input + "."); //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}

/*
* Determine the number of occurrences of the pad character that
will
* have to be added to the input string.
*/
int numPadChars = finalLength - input.length();

/*
* If the pad position is 'L' or 'l' for "leading", write the pad
* characters followed by the input string. Otherwise, write the pad
* characters after the input string.
*/
StringBuffer strbuffPaddedString = new StringBuffer();
if (padPosition == 'L' | padPosition == 'l') {
for (int ix = 0; ix < numPadChars; ix++)
strbuffPaddedString.append(padChar);
strbuffPaddedString.append(input);
} else {
strbuffPaddedString.append(input);
for (int ix = 0; ix < numPadChars; ix++)
strbuffPaddedString.append(padChar);
}

return (strbuffPaddedString.toString());
}
}---------------------------------------------------------------------------------------------------------------------

Stefan wrote:
Hi,
is there a way to get informations from tabelspaces of a db2
(datafiles) via java (jdbc)?

Thanx...
Stefan

Jun 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Annonce83 | last post by:
Good evening I will like to create a software on the grayline, I looking for information for this subject (calculation, code etc...) Thank your very much for your help. My best regards, ...
2
by: Lionel | last post by:
Hi all, I would like having more informations on how we could exchange informations and/or objects between PERL and JAVA. We have a Java programs that open, maintain and close telnet...
1
by: Gabriele *Darkbard* Farina | last post by:
Hi, there is a Python library that makes me able to extract outline informations from font files? I'd like to manage TrueType and FreeType fonts ... I searched for som wrappers, but I didn't...
0
by: Hermione | last post by:
Hi I want to work for a new application for the Pocket PC. I'd like to know about the following points: 1- Can I create an Access Database and transfert it to the pocket PC and access it ...
3
by: Olivier | last post by:
i need to create a multilinguage site and i need help : first i want to know what is the best and simple solution for a multilinguage site with plone 2? i want some tutorial, how to and if...
4
by: midnighthell | last post by:
I'm trying to get informations about my HDD, and I plan to use DeviceIoControl, but i don't understand how to use it,: First of all Function asks me to sign dwIoControlCode (this is...
3
by: John Holgerson | last post by:
Hi, is it possible to get informations about the exception that occured in that method?? (global.asax) protected void Application_Error(Object sender, System.EventArgs e) { } thanks for...
3
by: Ravi Ambros Wallau | last post by:
Hi: I would like to have some informations about my current ASP.NET application: 1. Have access to all instances of HttpSessionState that belongs to my application; Not only name or GUID, but...
1
by: durumdara | last post by:
Hi ! WXP, Py2.4.3. I want to get localized informations like month names, format parameters, etc. But nl_langinfo is not exists. Have the Python a way to get these informations in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.