以下是源程序:import java.sql.*;
import java.io.*;public class QueryPic {
static {
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
}
catch (Exception e) {
e.printStackTrace();
}
} private static PreparedStatement ps; public static Connection dbConnect() {
Connection con = null;
String url = ""; // URL is jdbc:db2:dbname
String dbname = "";
String uid = "";
String pwd = ""; try {
// connect with default userid and password
dbname = "NJCredit";
url = "jdbc:db2:" + dbname;
uid = "db2admin";
pwd = "db2admin";
con = DriverManager.getConnection(url, uid, pwd);
System.out.println(">Connected to " + dbname);
}
catch (Exception e) {
e.printStackTrace();
}
return con;
} public static void writePicture(Connection con) { String insert_picture = "insert into PICTURE(PICID,PICTUREINFO) VALUES(?,?)"; try { //create a new file object for the picture
File fil = new File("P000130.bmp"); //Creating byte[] data array to store the picture file
byte[] arrBytes = new byte[((new Long(fil.length())).intValue())]; System.out.println(arrBytes.length); //Initialising PreparedStatement ps with query
ps = con.prepareStatement(insert_picture);
ps.setString(1, "111");
ps.setBinaryStream(2, new ByteArrayInputStream(arrBytes),Integer.MAX_VALUE); //Execute the prepared statement
ps.execute(); }
catch (Exception ee) {
ee.printStackTrace();
}
System.out.println("Successfully inserted picture.jpg into "); } public static void readPicture(Connection con) {
int MAX_FILE_SIZE = 102400;
String fileName = "picture1.bmp";
try {
// create Statement object
Statement stmt = con.createStatement(); // create a ResultSet object by executing query
ResultSet rs = stmt.executeQuery("SELECT pictureinfo from picture"); // initialize number of records retrieved to 0
int recCount = 0; // create ResultMetaData object to verify target column's SQL datatype
ResultSetMetaData rsmd; // Retrieve the picture data from the database
while (rs.next()) {
System.out.println("Number of records = " + (++recCount)); String colName = new String("pictureinfo");
Blob blobObj = rs.getBlob(colName); if (blobObj == null) {
System.out.println("Error creating Blob object - Blob object is null");
}
else {
// Retrieve meta data for the result set
rsmd = rs.getMetaData();
// Print column's SQL datatype
System.out.println("Column's SQL type is : " +
rsmd.getColumnTypeName(1)); // Print the size of the picture data in the column
System.out.println("Length (size) of picture for empno=000130 is : " +
blobObj.length()); // Create byte[] data array to store picture data temporarily
// before writing it to a file
byte[] picData = new byte[MAX_FILE_SIZE]; // Retrieve the picture as a binary stream from the Blob object
InputStream is = blobObj.getBinaryStream(); // Store the binary stream from above into picData byte[] array
is.read(picData,0,(int)blobObj.length()); // Create output file object and point it to an output file to store
// the bitmap picture from the database
FileOutputStream picfile = new FileOutputStream(fileName); // Write the picture data - stored as a binary stream in the
// picData byte[] array - to the output bitmap file
picfile.write(picData); // Close the file object associated with the output bitmap file
picfile.close();
}
}
// Close resultset and statement objects
rs.close();
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
} // createTable()
public static void createTable(Connection con) {
try {
Statement stmt = con.createStatement();
stmt.execute("DROP TABLE PICTURE");
stmt.close();
}
catch (Exception e) { // ignore error, when table does not exist
} try {
Statement stmt = con.createStatement();
stmt.execute( " CREATE TABLE PICTURE( " +
" PICID CHAR(3) NOT NULL," +
" PICTUREINFO BLOB(102400) NOT NULL," +
" CONSTRAINT PK_PICID PRIMARY KEY (PICID));" + " COMMENT ON TABLE PICTURE IS '照片数据表';" + " COMMENT ON COLUMN PICTURE.PICID IS '照片ID';" +
" COMMENT ON COLUMN PICTURE.PICTUREINFO IS '照片信息';");
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
} public static void main(String argv[]) {
try {
// call DBConnect to connect to database
Connection con = dbConnect(); // call createTable() to create table Picture
createTable(con); //call writePicture to add picture record
writePicture(con); //call readPicture from database
readPicture(con); // Close connection object
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;public class QueryPic {
static {
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
}
catch (Exception e) {
e.printStackTrace();
}
} private static PreparedStatement ps; public static Connection dbConnect() {
Connection con = null;
String url = ""; // URL is jdbc:db2:dbname
String dbname = "";
String uid = "";
String pwd = ""; try {
// connect with default userid and password
dbname = "NJCredit";
url = "jdbc:db2:" + dbname;
uid = "db2admin";
pwd = "db2admin";
con = DriverManager.getConnection(url, uid, pwd);
System.out.println(">Connected to " + dbname);
}
catch (Exception e) {
e.printStackTrace();
}
return con;
} public static void writePicture(Connection con) { String insert_picture = "insert into PICTURE(PICID,PICTUREINFO) VALUES(?,?)"; try { //create a new file object for the picture
File fil = new File("P000130.bmp"); //Creating byte[] data array to store the picture file
byte[] arrBytes = new byte[((new Long(fil.length())).intValue())]; System.out.println(arrBytes.length); //Initialising PreparedStatement ps with query
ps = con.prepareStatement(insert_picture);
ps.setString(1, "111");
ps.setBinaryStream(2, new ByteArrayInputStream(arrBytes),Integer.MAX_VALUE); //Execute the prepared statement
ps.execute(); }
catch (Exception ee) {
ee.printStackTrace();
}
System.out.println("Successfully inserted picture.jpg into "); } public static void readPicture(Connection con) {
int MAX_FILE_SIZE = 102400;
String fileName = "picture1.bmp";
try {
// create Statement object
Statement stmt = con.createStatement(); // create a ResultSet object by executing query
ResultSet rs = stmt.executeQuery("SELECT pictureinfo from picture"); // initialize number of records retrieved to 0
int recCount = 0; // create ResultMetaData object to verify target column's SQL datatype
ResultSetMetaData rsmd; // Retrieve the picture data from the database
while (rs.next()) {
System.out.println("Number of records = " + (++recCount)); String colName = new String("pictureinfo");
Blob blobObj = rs.getBlob(colName); if (blobObj == null) {
System.out.println("Error creating Blob object - Blob object is null");
}
else {
// Retrieve meta data for the result set
rsmd = rs.getMetaData();
// Print column's SQL datatype
System.out.println("Column's SQL type is : " +
rsmd.getColumnTypeName(1)); // Print the size of the picture data in the column
System.out.println("Length (size) of picture for empno=000130 is : " +
blobObj.length()); // Create byte[] data array to store picture data temporarily
// before writing it to a file
byte[] picData = new byte[MAX_FILE_SIZE]; // Retrieve the picture as a binary stream from the Blob object
InputStream is = blobObj.getBinaryStream(); // Store the binary stream from above into picData byte[] array
is.read(picData,0,(int)blobObj.length()); // Create output file object and point it to an output file to store
// the bitmap picture from the database
FileOutputStream picfile = new FileOutputStream(fileName); // Write the picture data - stored as a binary stream in the
// picData byte[] array - to the output bitmap file
picfile.write(picData); // Close the file object associated with the output bitmap file
picfile.close();
}
}
// Close resultset and statement objects
rs.close();
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
} // createTable()
public static void createTable(Connection con) {
try {
Statement stmt = con.createStatement();
stmt.execute("DROP TABLE PICTURE");
stmt.close();
}
catch (Exception e) { // ignore error, when table does not exist
} try {
Statement stmt = con.createStatement();
stmt.execute( " CREATE TABLE PICTURE( " +
" PICID CHAR(3) NOT NULL," +
" PICTUREINFO BLOB(102400) NOT NULL," +
" CONSTRAINT PK_PICID PRIMARY KEY (PICID));" + " COMMENT ON TABLE PICTURE IS '照片数据表';" + " COMMENT ON COLUMN PICTURE.PICID IS '照片ID';" +
" COMMENT ON COLUMN PICTURE.PICTUREINFO IS '照片信息';");
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
} public static void main(String argv[]) {
try {
// call DBConnect to connect to database
Connection con = dbConnect(); // call createTable() to create table Picture
createTable(con); //call writePicture to add picture record
writePicture(con); //call readPicture from database
readPicture(con); // Close connection object
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货