Welcome To: JDBC Programming I
Welcome To: JDBC Programming I
Welcome To: JDBC Programming I
JDBC Programming I
IBM
Unit Objectives
5. Receive results
import java.sql.*; 1
import java.io.*;
import java.lang.*;
class MyJDBC {
static {
2 try { Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
} catch (Exception e) {
e.printStackTrace();
}
Connect to Database
3 { con = DriverManager.getConnection(url); }
else if (argv.length == 2)
{ String userid = argv[0];
String passwd = argv[1];
4 con = DriverManager.getConnection(url,userid,passwd);
}
else
{ throw new Exception
("\n Usage: java MyJDBC[,username,password]\n");}
SELECT Statement
3 while ( rs.next() ) {
4 System.out.println("empno = " + rs.getString(1) +
"lastname = " + rs.getString(2) );
}
rs.close();
5
stmt.close();
6
con.close();
7
} catch (Exception e) {
e.printStackTrace();
8
}
UPDATE Statement
GRAPHIC CHAR
FLOAT
Java
DB2
FORTRAN REFERENCE
LIBRARY
VARCHAR
DECIMAL COBOL
INTEGER
Declaring Host Variables - Java Program
Languages supported
C, Java, COBOL, FORTRAN
Java example:
S
Q Information / SQLCODE
Program L
C
A
Executable SQL
DB2
SQLCA Codes
INTEGER CHAR(1)
CONDITION SQLCODE SQLWARNO REQUEST STATUS
<0
ERROR FAILED
(MORE)
+100 DATA NOT FOUND
NOTFOUND
SQLSTATE = "02000"
SQLCODE detailed
possibly platform dependent
numeric
PROGRAM:
SQLCA NUMBER
SQLCODE OF ROWS
SQLWARN0 AFFECTED
SQLERRD[2] DB2
INSERT T
E S
U
E Q
UPDATE R
DELETE
Error Processing
try {
sql = "DELETE FROM TEMPL WHERE EMPNO = 999";
psstmt = con.prepareStatement(sql);
1 deleteCount = psstmt.executeUpdate();
2 if (( SQLWarn = psstmt.getWarnings()) !=null)
{
System.out.println ("\n Warning received on a DELETE \n");}
} // end try
3 catch (SQLException x)
4 { if (x.getSQLState().equals("42818"))
{ System.out.println ("\n Operand data types not compatible \n");
} // end if
else { System.out.println ("\n Error deleting from TEMPL \n");
System.out.println ("\n Value of SQLCODE is: \n");
5 SQLCode = x.getErrorCode();
System.out.println (SQLCode);
} // end else
} // end catch
JDBC and Security
JDBC
DELETE FROM
TEMPL WHERE
ID = '123'