-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectDB_ERR.java
More file actions
152 lines (133 loc) · 4.83 KB
/
SelectDB_ERR.java
File metadata and controls
152 lines (133 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.nio.charset.Charset;
import java.nio.charset.*;
import java.sql.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import javax.xml.bind.DatatypeConverter;
public class SelectDB_ERR
{
public static String hexToString(String codepage, String hexString)
{
Charset charset = Charset.forName(codepage);
CharsetDecoder decoder = charset.newDecoder();
try {
byte[] bytes = DatatypeConverter.parseHexBinary(hexString);
ByteBuffer buff = ByteBuffer.wrap(bytes);
CharBuffer cbuf = decoder.decode(buff);
String s = cbuf.toString();
return s;
}
catch (CharacterCodingException e){
System.out.println("Exception: " + e);
e.printStackTrace();
String errStr = "E";
return errStr;
}
catch (Exception e){
System.out.println("Exception: " + e);
e.printStackTrace();
String errStr = "Unkown err";
return errStr;
}
}
public static void main(String[] args)
{
String urlPrefix = "jdbc:db2:";
String url;
String user;
String password;
String str1;
String hexValue;
String str3;
String defaultCharset;
Connection con;
Statement stmt;
ResultSet rs;
System.out.println ("**** Enter class EzJava");
// Check the that first argument has the correct form for the portion
// of the URL that follows jdbc:db2:,
// as described
// in the Connecting to a data source using the DriverManager
// interface with the IBM Data Server Driver for JDBC and SQLJ topic.
// For example, for IBM Data Server Driver for
// JDBC and SQLJ type 2 connectivity,
// args[0] might be MVS1DB2M. For
// type 4 connectivity, args[0] might
// be //stlmvs1:10110/MVS1DB2M.
if (args.length!=3)
{
System.err.println ("Invalid value. First argument appended to "+
"jdbc:db2: must specify a valid URL.");
System.err.println ("Second argument must be a valid user ID.");
System.err.println ("Third argument must be the password for the user ID.");
System.exit(1);
}
url = urlPrefix + args[0];
user = args[1];
password = args[2];
try
{
// Load the driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("**** Loaded the JDBC driver");
defaultCharset = Charset.defaultCharset().displayName();
System.out.println("Charset.defaultCharset().displayName() = "+defaultCharset);
// Create the connection using the IBM Data Server Driver for JDBC and SQLJ
con = DriverManager.getConnection (url, user, password);
// Commit changes manually
con.setAutoCommit(false);
System.out.println("**** Created a JDBC connection to the data source");
// Create the Statement
stmt = con.createStatement();
System.out.println("**** Created JDBC Statement object");
// Execute a query and generate a ResultSet instance
rs = stmt.executeQuery("select a,hex(a) from nrsinst.euckr");
// rs = stmt.executeQuery("select binary(a),hex(a) from nrsinst.euckr");
System.out.println("**** Created JDBC ResultSet object");
Charset charset = Charset.forName("MS949");
CharsetDecoder decoder = charset.newDecoder();
// Print all of the employee numbers to standard output device
while (rs.next()) {
str1 = rs.getString(1);
hexValue = rs.getString(2);
String stringMS949 = hexToString("MS949", hexValue);
System.out.println("value = " + str1 + " , hex = " + hexValue + ", MS949 = " + stringMS949 );
}
System.out.println("**** Fetched all rows from JDBC ResultSet");
// Close the ResultSet
rs.close();
System.out.println("**** Closed JDBC ResultSet");
// Close the Statement
stmt.close();
System.out.println("**** Closed JDBC Statement");
// Connection must be on a unit-of-work boundary to allow close
con.commit();
System.out.println ( "**** Transaction committed" );
// Close the connection
con.close();
System.out.println("**** Disconnected from data source");
System.out.println("**** JDBC Exit from class EzJava - no errors");
}
catch (ClassNotFoundException e)
{
System.err.println("Could not load JDBC driver");
System.out.println("Exception: " + e);
e.printStackTrace();
}
catch(SQLException ex)
{
System.err.println("SQLException information");
while(ex!=null) {
System.err.println ("Error msg: " + ex.getMessage());
System.err.println ("SQLSTATE: " + ex.getSQLState());
System.err.println ("Error code: " + ex.getErrorCode());
ex.printStackTrace();
ex = ex.getNextException(); // For drivers that support chained exceptions
}
}
catch(Exception ex){
System.err.println ("Error msg: " + ex.getMessage());
ex.printStackTrace();
}
}
}