More Related Content Similar to This is a basic JAVA pgm that contains all of the major compoents of DB2 Similar to This is a basic JAVA pgm that contains all of the major compoents of DB2 (20) This is a basic JAVA pgm that contains all of the major compoents of DB21. 1
/* This is a basic JAVA pgm that contains all of the major compoents of DB2/SQL table processing that can be copied for a the project */
import java.sql.*;
public class DatabaseProcessing{
private static final String JDBC_DRIVER = "com.ibm.db2.jcc.DB2Driver";
private static final String DATABASE_URL = "jdbc:db2://00.000.00.00:00000/TEST";
private static final String USERNAME = "username";
private static final String PASSWORD = "PassWord";
/* creates the table in the database for testing which otherwise have to be created through DMEM */
public static void createTable(){
Connection connection = null;
Statement statement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL,USERNAME,
PASSWORD);
statement = connection.createStatement();
2. 2
//boolean b=statement.execute("DROP TABLE IF EXISTS tblname");
boolean b=statement.execute("CREATE TABLE tblname(fld1 int primary key, name varchar(15),department int,fld2 int,location
varchar(20))");
if(b==true)
System.out.println("Table created...");
} catch (SQLException sqlEx) {
System.out.println("SQLException while creating test table " + "database table: " + sqlEx.toString());
System.exit(1);
} catch (ClassNotFoundException clsNotFoundEx) {
System.out.println(" classes the program is trying to call does not exist; table " + "database table: " + clsNotFoundEx.toString());
System.exit(2);
} finally {
try {
statement.close();
connection.close();
} catch (Exception e) {
System.out.println( "Major Error - Hard Stop " + e.toString() + e.getMessage ());
System.exit(3);
}
3. 3
}
}
/* insert into table */
public static void inserttblname(int fld1, String desc,int code, int fld2, String city){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
preparedStatement = connection.prepareStatement("INSERT INTO tblname VALUES(?,?,?,?,?)");
preparedStatement.setInt(1,fld1);
preparedStatement.setString(2, desc);
preparedStatement.setInt(3,code);
preparedStatement.setInt(4,fld2);
preparedStatement.setString(5, city);
boolean b=preparedStatement.execute();
if(b==true)
4. 4
System.out.println("1 record inserted...");
} catch (SQLException sqlEx) {
System.out.println("SQLException ; INSERT into table " + "database table: " + sqlEx.toString());
System.exit(-2);
} catch (ClassNotFoundException clsNotFoundEx) {
System.out.println(" classes the program is trying to call does not exist; table " + "database table: " + clsNotFoundEx.toString());
System.exit(-3);
} finally {
try {
preparedStatement.close();
connection.close();
} catch (Exception e) {
System.out.println( "Major Error - Hard Stop " + e.toString() + e.getMessage ());
System.exit(-4);
}
}
}
/* update column in table */
public static void updateFld2 (int fld1, int addmore){
5. 5
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL, USERNAME,PASSWORD);
preparedStatement = connection.prepareStatement("UPDATE tblname SET fld2=fld2+? WHERE fld1=?");
preparedStatement.setInt(1,addmore);
preparedStatement.setInt(2,fld1);
boolean b=preparedStatement.execute();
if(b==true)
System.out.println("$"+addmore+" addmore for record fld1="+fld1);
} catch (SQLException sqlEx) {
System.out.println("SQLException; UPDATE into table " + "database table: " + sqlEx.toString());
System.exit(1);
} catch (ClassNotFoundException clsNotFoundEx) {
System.out.println(" classes the program is trying to call does not exist; table " + "database table: " + clsNotFoundEx.toString());
System.out.println("SQLException while updating the " + "database table: " + clsNotFoundEx.toString());
System.exit(-5);
} finally {
6. 6
try {
preparedStatement.close();
connection.close();
} catch (Exception e) {
System.out.println( "Major Error - Hard Stop " + e.toString() + e.getMessage ());
System.exit(-6);
}
}
}
/* delete from table. where clause is manadatory */
public static void deletetblname(int fld1){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
preparedStatement = connection.prepareStatement("DELETE FROM tblname WHERE fld1=?");
preparedStatement.setInt(1,fld1);
boolean b=preparedStatement.execute();
7. 7
if(b==true)
System.out.println("1 record deleted...");
} catch (SQLException sqlEx) {
System.out.println("SQLException ; DELETE table " + "database table: " + sqlEx.toString());
System.exit(1);
} catch (ClassNotFoundException clsNotFoundEx) {
System.out.println(" classes the program is trying to call does not exist; table " + "database table: " + clsNotFoundEx.toString());
System.out.println("SQLException while deleting the " + "database table: " + clsNotFoundEx.toString());
System.exit(-7);
} finally {
try {
preparedStatement.close();
connection.close();
} catch (Exception e) {
System.out.println( "Major Error - Hard Stop " + e.toString() + e.getMessage ());
System.exit(-8);
}
}
}
8. 8
/* read from table into resutls area - similar to COBOL cursor w/ Select all */
public static void readtblname(){
Connection connection = null;
Statement statement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL,USERNAME,
PASSWORD);
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM tblname");
while (resultSet.next()) {
System.out.println("PrimaryKey:t" + resultSet.getString(1));
}
} catch (SQLException sqlEx) {
System.out.println("SQLException ; READ from table " + "database table: " + sqlEx.toString());
System.exit(-9);
} catch (ClassNotFoundException clsNotFoundEx) {
System.out.println(" classes the program is trying to call does not exist; table " + "database table: " + clsNotFoundEx.toString());
9. 9
System.out.println("SQLException while reading the " + "database table: " + clsNotFoundEx.toString());
System.exit(-10);
} finally {
try {
statement.close();
connection.close();
} catch (Exception e) {
System.out.println( "Major Error - Hard Stop " + e.toString() + e.getMessage ());
System.exit(-11);
}
}
}
/* read from table via Primary Key/field into resutls area - similar to COBOL cursor */
public static void readtblname(int fld1) {
Connection connection = null;
Statement statement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL,USERNAME,
10. 10
PASSWORD);
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM tblname WHERE fld1="+fld1);
System.out.println();
while (resultSet.next()) {
System.out.println("PrimaryKey:t" + resultSet.getString(1));
}
} catch (SQLException sqlEx) {
while(sqlEx != null) {
System.err.println("SQLException information");
System.err.println("Error msg: " + sqlEx.getMessage());
System.err.println("SQLSTATE: " + sqlEx.getSQLState());
System.err.println("Error code: " + sqlEx.getErrorCode());
sqlEx=sqlEx.getNextException();
}
System.exit(-12);
} catch (ClassNotFoundException clsNotFoundEx) {
System.out.println(" classes the program is trying to call does not exist; table " + "database table: " + clsNotFoundEx.toString());
11. 11
System.out.println("SQLException while reading the " + "database table: " + clsNotFoundEx.toString());
System.exit(-13);
} finally {
try {
statement.close();
connection.close();
} catch (Exception e) {
System.out.println( "Major Error - Hard Stop " + e.toString() + e.getMessage ());
System.exit(-14);
}
}
}
/* start of main processing flow */
public static void main(String[] args) {
createTable();
inserttblname(30031, "Peach", 404, 2500, "Atlanta");
inserttblname(30032, "Orange", 678, 2600, "Stone Mountain");
inserttblname(30033, "Banana", 770, 2700, "Conyers");
inserttblname(30034, "Plum", 770, 2800, "Marietta");
12. 12
inserttblname(30035, "Apple", 404, 2900, "College Park");
inserttblname(30036, "Bananna", 678, 3000, "Macon");
updateFld2 (30034, 1000);
inserttblname(30099, "Grape", 999, 4000, "Decatur");
deletetblname(30036);
readtblname();
readtblname(30099);
}
}