SlideShare a Scribd company logo
1 of 12
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
//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
}
}
/* 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
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
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
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
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
/* 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
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
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
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
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);
}
}

More Related Content

What's hot

Introduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsIntroduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsJason Myers
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMJason Myers
 
知っておきたいSpring Batch Tips
知っておきたいSpring Batch Tips知っておきたいSpring Batch Tips
知っておきたいSpring Batch Tipsikeyat
 
Database administration commands
Database administration commands Database administration commands
Database administration commands Varsha Ajith
 
PyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialPyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialjbellis
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6MYXPLAIN
 
Data20161007
Data20161007Data20161007
Data20161007capegmail
 
Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sqlsaritasingh19866
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql JOYITAKUNDU1
 
Js 单元测试框架介绍
Js 单元测试框架介绍Js 单元测试框架介绍
Js 单元测试框架介绍louieuser
 
你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能maclean liu
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321maclean liu
 
Salesforce, APEX Concepts
Salesforce, APEX ConceptsSalesforce, APEX Concepts
Salesforce, APEX ConceptsGaurish Goel
 
The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202Mahmoud Samir Fayed
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assertfangjiafu
 

What's hot (20)

Introduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsIntroduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic Migrations
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORM
 
知っておきたいSpring Batch Tips
知っておきたいSpring Batch Tips知っておきたいSpring Batch Tips
知っておきたいSpring Batch Tips
 
Oracle ORA Errors
Oracle ORA ErrorsOracle ORA Errors
Oracle ORA Errors
 
Database administration commands
Database administration commands Database administration commands
Database administration commands
 
PyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialPyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorial
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
 
Data20161007
Data20161007Data20161007
Data20161007
 
Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sql
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql
 
Js 单元测试框架介绍
Js 单元测试框架介绍Js 单元测试框架介绍
Js 单元测试框架介绍
 
Sequelize
SequelizeSequelize
Sequelize
 
你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
 
Library system project file
Library system project fileLibrary system project file
Library system project file
 
Salesforce, APEX Concepts
Salesforce, APEX ConceptsSalesforce, APEX Concepts
Salesforce, APEX Concepts
 
The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202
 
Twitter codeigniter library
Twitter codeigniter libraryTwitter codeigniter library
Twitter codeigniter library
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assert
 

Viewers also liked (17)

Briefing UP Creative
Briefing UP CreativeBriefing UP Creative
Briefing UP Creative
 
Photos
PhotosPhotos
Photos
 
Peer Panel Program Fall 15
Peer Panel Program Fall 15Peer Panel Program Fall 15
Peer Panel Program Fall 15
 
20131108 - Big Data presentation at the 2nd GDG Brunei Darussalam DevFest
20131108 - Big Data presentation at the 2nd GDG Brunei Darussalam DevFest20131108 - Big Data presentation at the 2nd GDG Brunei Darussalam DevFest
20131108 - Big Data presentation at the 2nd GDG Brunei Darussalam DevFest
 
κόκκινη τελεία
κόκκινη τελεία κόκκινη τελεία
κόκκινη τελεία
 
JAtkins Resume
JAtkins ResumeJAtkins Resume
JAtkins Resume
 
Acciones uso eficiente del agua
Acciones uso eficiente del aguaAcciones uso eficiente del agua
Acciones uso eficiente del agua
 
Retail Focus Program Spring 2016
Retail Focus Program Spring 2016Retail Focus Program Spring 2016
Retail Focus Program Spring 2016
 
Project by daniel aragón pérez
Project by daniel aragón pérezProject by daniel aragón pérez
Project by daniel aragón pérez
 
Dianaleydi
DianaleydiDianaleydi
Dianaleydi
 
CALENDARI D'ADVENT
CALENDARI D'ADVENTCALENDARI D'ADVENT
CALENDARI D'ADVENT
 
Progression powerpoint
Progression powerpoint Progression powerpoint
Progression powerpoint
 
Raccordi per alte pressioni - Fitok Serie 10
Raccordi per alte pressioni - Fitok Serie 10Raccordi per alte pressioni - Fitok Serie 10
Raccordi per alte pressioni - Fitok Serie 10
 
Informacion oferta modular_parcial_web
Informacion oferta modular_parcial_webInformacion oferta modular_parcial_web
Informacion oferta modular_parcial_web
 
Oficina de Teatro
Oficina de TeatroOficina de Teatro
Oficina de Teatro
 
Misura scariche parziali nei macchinari rotanti - Analizzatori Portatili
Misura scariche parziali nei macchinari rotanti - Analizzatori PortatiliMisura scariche parziali nei macchinari rotanti - Analizzatori Portatili
Misura scariche parziali nei macchinari rotanti - Analizzatori Portatili
 
Aprendizaje Autónomo
Aprendizaje AutónomoAprendizaje Autónomo
Aprendizaje Autónomo
 

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)

My java file
My java fileMy java file
My java file
 
Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1
 
Db examples
Db examplesDb examples
Db examples
 
Refactoring Jdbc Programming
Refactoring Jdbc ProgrammingRefactoring Jdbc Programming
Refactoring Jdbc Programming
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
 
Jdbc Odbc使用Excel作数据源
Jdbc Odbc使用Excel作数据源Jdbc Odbc使用Excel作数据源
Jdbc Odbc使用Excel作数据源
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
JDBC for CSQL Database
JDBC for CSQL DatabaseJDBC for CSQL Database
JDBC for CSQL Database
 
Jdbc ja
Jdbc jaJdbc ja
Jdbc ja
 
Import java
Import javaImport java
Import java
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.ppt
 
JDBC (2).ppt
JDBC (2).pptJDBC (2).ppt
JDBC (2).ppt
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc tutorial
Jdbc tutorialJdbc tutorial
Jdbc tutorial
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
 
Scrollable Test App
Scrollable Test AppScrollable Test App
Scrollable Test App
 

This is a basic JAVA pgm that contains all of the major compoents of DB2

  • 1. 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); } }