Assignment 5.Docx
Assignment 5.Docx
PRN:22210574
AIM: Build an application on z/OS using java and integrate it with DB2
Type db2.
list db2 directory will give list of all databases available. The default one is ‘SAMPLE’.
The database can be created using command – db2 create database <dbname>
Connect to the desired db using connect to <dbname> and obtain the IP address.
Default port for db2 is 50000 but it might be different in some cases.(as here it’s 25000)
4)For windows, the DB2 server takes the windows authentication as the default one.
So your windows username and password is the credentials for your db2, too.
The JDBC_URL="jdbc:db2://localhost:25000/USER";
Where hostname=”localhost”
Port=25000
Dbname=USER
Class.forName("com.ibm.db2.jcc.DB2Driver");
Db2JdbcTest.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
try {
// Load the DB2 JDBC driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException e) {
System.err.println("DB2 JDBC Driver not found. Include the driver in
your classpath.");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("SQL exception occurred while trying to connect to
the DB2 database.");
e.printStackTrace();
} finally {
// Close the connection
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
System.err.println("Failed to close the connection.");
e.printStackTrace();
}
}
}
}
Where C:\Program Files\IBM\SQLLIB\java\db2jcc4.jar is the path where you unzipped the Jar
file.(from JDBC driver)
Output:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
try {
// Load the DB2 JDBC driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("Connecting to database...");
connection = DriverManager.getConnection(JDBC_URL, props);
statement = connection.createStatement();
try {
statement.executeUpdate("DROP TABLE DOCTORS");
System.out.println("Dropped existing DOCTORS table");
} catch (SQLException e) {
System.out.println("DOCTORS table does not exist yet");
}
try {
statement.executeUpdate("DROP TABLE HOSPITALS");
System.out.println("Dropped existing HOSPITALS table");
} catch (SQLException e) {
System.out.println("HOSPITALS table does not exist yet");
}
statement.executeUpdate(createHospitalsTable);
System.out.println("Created HOSPITALS table");
statement.executeUpdate(createDoctorsTable);
System.out.println("Created DOCTORS table");
statement.executeUpdate(createPatientsTable);
System.out.println("Created PATIENTS table");
} catch (ClassNotFoundException e) {
System.err.println("DB2 JDBC Driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("SQL exception occurred:");
e.printStackTrace();
} finally {
// Close resources
try {
if (statement != null) statement.close();
if (connection != null) connection.close();
System.out.println("Database resources closed.");
} catch (SQLException e) {
System.err.println("Failed to close database resources.");
e.printStackTrace();
}
}
}
}
This will create the tables in USER Database. We can also use the ‘SYSTEM’(default) database.
Output:
HospitalDataViewer.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
try {
// Load the DB2 JDBC driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("Connecting to database...");
connection = DriverManager.getConnection(JDBC_URL, props);
statement = connection.createStatement();
} catch (ClassNotFoundException e) {
System.err.println("DB2 JDBC Driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("SQL exception occurred:");
e.printStackTrace();
} finally {
// Close resources
try {
if (statement != null) statement.close();
if (connection != null) connection.close();
System.out.println("Database resources closed.");
} catch (SQLException e) {
System.err.println("Failed to close database resources.");
e.printStackTrace();
}
}
}
/**
* Utility method to display a ResultSet in a formatted table
*/
private static void displayResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
CONCLUSION: We successfully connected DB2 with Java using JDBC driver and DB2 server.
Also ,we were able to create the databases and tables and perform CRUD operations on it.