JDBC-ODBCブリッジ で SELECT(CSV形式で標準出力に表示) ▲TOP
import java.io.*;
import java.sql.*;
class odbc_select01 {
public odbc_select01() {
String uid = "uid";
String pwd = "pwd";
String url = "jdbc:odbc:dsn";
String query = "select * from kemp";
try {
// jdbc-odbcブリッジドライバをロード
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// 接続
Connection con = DriverManager.getConnection (url, uid, pwd );
Statement stmt = con.createStatement ();
// 実行
query = new String(query.getBytes(), "JISAutoDetect");
ResultSet rs = stmt.executeQuery (query);
// 取得結果をCSV形式で標準出力に表示
int i;
ResultSetMetaData rsmd = rs.getMetaData ();
int numCols = rsmd.getColumnCount ();
// 列名を表示
String delim = "";
for (i=1; i<=numCols; i++) {
String mystr = rsmd.getColumnLabel(i);
if (mystr == null) mystr = "";
if (i == numCols) {
delim = "";
} else {
delim = ",";
}
System.out.print(mystr + delim);
}
System.out.print("\n");
// 取得結果を表示
boolean more = rs.next ();
while (more) {
for (i=1; i<=numCols; i++) {
String mystr = rs.getString(i);
if (mystr == null) mystr = "";
if (i == numCols) {
delim = "";
} else {
delim = ",";
}
System.out.print(mystr + delim);
}
System.out.print("\n");
more = rs.next ();
}
rs.close();
stmt.close();
con.close();
} catch (SQLException ex) {
String mystr = "";
while (ex != null) {
mystr = mystr + ex.getMessage() + "\n";
System.out.print(mystr);
ex = ex.getNextException ();
}
System.out.print("SQL Exception: " + mystr);
} catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
}
public static void main(String args[]) {
odbc_select01 je = new odbc_select01();
}
}
JDBC-ODBCブリッジ で ストアドプロシージャのOUTパラメータ取得 ▲TOP
import java.io.*;
import java.sql.*;
class odbc_exec01 {
public odbc_exec01() {
String uid = "uid";
String pwd = "pwd";
String url = "jdbc:odbc:dsn";
try {
// jdbc-odbcブリッジドライバをロード
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// 接続
Connection con = DriverManager.getConnection (url, uid, pwd );
// CallableStatement
// up_outp (up_outp @ivar IN varchar(30) , @ovar OUT varchar(30), @ovar2 OUT int)
CallableStatement cstmt = con.prepareCall("{call up_outp (?,?,?)}");
// パラメータセット
cstmt.setString(1, "123456"); //INパラメータ
cstmt.registerOutParameter(2, Types.VARCHAR, 30); //OUTパラメータ1
cstmt.registerOutParameter(3, Types.INTEGER); //OUTパラメータ2
// 実行
cstmt.execute();
// 結果表示
System.out.println( "Result: " + cstmt.getString(2)); //OUTパラメータ1
System.out.println( "Result: " + cstmt.getInt(3)); //OUTパラメータ2
cstmt.close();
con.close();
} catch (SQLException ex) {
String mystr = "";
while (ex != null) {
mystr = mystr + ex.getMessage() + "\n";
System.out.print(mystr);
ex = ex.getNextException ();
}
System.out.print("SQL Exception: " + mystr);
} catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
}
public static void main(String args[]) {
odbc_exec01 je = new odbc_exec01();
}
}