11: accessLOB
package com.github.ryan6073.toLearn;
import javax.swing.*;
import java.sql.*;
public class accessLOB {
Connection connection = null;
public accessLOB() {
connection = connectToDB();
try {
run();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void run() throws SQLException {
String resume = null;
String empnum = "000130";
int startper = 0, startper1, startdpt = 0;
PreparedStatement stmt1, stmt2, stmt3 = null;
String sql1, sql2, sql3 = null;
String empno, resumefmt = null;
Clob resumelob = null;
ResultSet rs1, rs2, rs3 = null;
sql1 = "SELECT POSSTR(RESUME, 'Personal') "
+ "FROM JLU.EMP_RESUME "
+ "WHERE EMPNO = ? AND RESUME_FORMAT = 'ascii' ";
stmt1 = connection.prepareStatement(sql1);
stmt1.setString(1, empnum);
rs1 = stmt1.executeQuery();
while (rs1.next()) {
startper = rs1.getInt(1);
}
sql2 = "SELECT POSSTR(RESUME, 'Department') "
+ "FROM JLU.EMP_RESUME "
+ "WHERE EMPNO = ? AND RESUME_FORMAT = 'ascii' ";
stmt2 = connection.prepareStatement(sql2);
stmt2.setString(1, empnum);
rs2 = stmt2.executeQuery();
while (rs2.next()) {
startdpt = rs2.getInt(1);
}
startper1 = startper - 1;
sql3 = "SELECT EMPNO, RESUME_FORMAT,"
+ "SUBSTR(RESUME, 1, ?) || SUBSTR(RESUME, ?) AS RESUME "
+ "FROM JLU.EMP_RESUME "
+ "WHERE EMPNO = ? AND RESUME_FORMAT = 'ascii' ";
stmt3 = connection.prepareStatement(sql3);
stmt3.setInt(1, startper1);
stmt3.setInt(2, startdpt);
stmt3.setString(3, empnum);
rs3 = stmt3.executeQuery();
while (rs3.next()) {
empno = rs3.getString(1);
resumefmt = rs3.getString(2);
resumelob = rs3.getClob(3);
long len = resumelob.length();
int len1 = (int) len;
String resumeout = resumelob.getSubString(1, len1);
System.out.println("员工编号:" + empno);
System.out.println("简历格式:" + resumefmt);
System.out.println("简历内容:" + resumeout);
}
}
private Connection connectToDB() {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
connection = DriverManager.getConnection(
"jdbc:db2://192.168.80.128:50000/sample",
"db2admin",
"db2admin"
);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "连接数据库失败:" + e.getMessage());
}
return connection;
}
public static void main(String[] args) {
new accessLOB();
}
}
运行结果:
参与讨论
(Participate in the discussion)
参与讨论