1. CLOB -- Insert
Class UtiDb
Class Book
package JavaJDBC.model;
import java.io.File;
/**
* Created by linux_ccmj on 16-5-10.
*/
public class Book {
private int idBoo;
private String namBoo;
private float priBoo;
private String namAut;
private int idTypBoo;
private File context;
public Book(int idBoo, String namBoo, float priBoo, String namAut, int idTypBoo) {
this.idBoo = idBoo;
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
}
public Book(String namBoo, float priBoo, String namAut, int idTypBoo) {
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
}
public Book(String namBoo, float priBoo, String namAut, int idTypBoo, File context) {
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
this.context = context;
}
public int getIdBoo() {
return idBoo;
}
public void setIdBoo(int idBoo) {
this.idBoo = idBoo;
}
public String getNamBoo() {
return namBoo;
}
public void setNamBoo(String namBoo) {
this.namBoo = namBoo;
}
public float getPriBoo() {
return priBoo;
}
public void setPriBoo(float priBoo) {
this.priBoo = priBoo;
}
public String getNamAut() {
return namAut;
}
public void setNamAut(String namAut) {
this.namAut = namAut;
}
public int getIdTypBoo() {
return idTypBoo;
}
public void setIdTypBoo(int idTypBoo) {
this.idTypBoo = idTypBoo;
}
public File getContext() {
return context;
}
public void setContext(File context) {
this.context = context;
}
@Override
public String toString() {
return "[ "+this.idBoo+", "+this.namBoo+", "+this.priBoo+", "+this.namAut+", "+this.idTypBoo+" ]";
}
}
Class CLOB
package JavaJDBC.Chap06.sec01;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* Created by linux_ccmj on 16-5-16.
*/
public class CLOB {
// utiDb
private static UtiDb utiDb = new UtiDb();
// Insert fun
private static int insBooFun(Book boo) throws Exception{
// Connection, string sql command, prepared statement
String strComSql = "insert into t_book2 values(null, ?, ?, ?, ?, ?)";
Connection con = utiDb.getConFun();
PreparedStatement staPre = con.prepareStatement(strComSql);
// Prepared statement set value, file ...
File context = boo.getContext();
InputStream texInpStr = new FileInputStream(context);
staPre.setString(1, boo.getNamBoo());
staPre.setFloat(2, boo.getPriBoo());
staPre.setString(3, boo.getNamAut());
staPre.setInt(4, boo.getIdTypBoo());
staPre.setAsciiStream(5, texInpStr, context.length());
int marRes = staPre.executeUpdate();
// Close
utiDb.clsConFun(staPre, con);
// Return
return marRes;
}
// Test main
public static void main(String[] args) throws Exception{
// Book
File context = new File("/home/linux_ccmj/Documents/ComputerLearning/Web/峰哥体系/1_一头扎进JDBC视频教程/一头扎进JDBC视频教程源码/6_Chapter/helloWorld.txt");
Book boo = new Book("暖通空调", 77, "中国建筑协会", 3, context);
// Insert book
int marRes = insBooFun(boo);
if (marRes==1){
System.out.println("成功插入一组数据!");
}
else {
System.out.println("未插入数据!");
}
}
}
2. CLOB -- Get Info
Class UtiDb
Class Book
Class CLOB
package JavaJDBC.Chap06.sec01;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* Created by linux_ccmj on 16-5-16.
*/
public class CLOB {
// utiDb
private static UtiDb utiDb = new UtiDb();
// Insert fun
/**
* Insert book into t_book2
* @param boo
* @return
* @throws Exception
*/
private static int insBooFun(Book boo) throws Exception{
// Connection, string sql command, prepared statement
String strComSql = "insert into t_book2 values(null, ?, ?, ?, ?, ?)";
Connection con = utiDb.getConFun();
PreparedStatement staPre = con.prepareStatement(strComSql);
// Prepared statement set value, file ...
File context = boo.getContext();
InputStream texInpStr = new FileInputStream(context);
staPre.setString(1, boo.getNamBoo());
staPre.setFloat(2, boo.getPriBoo());
staPre.setString(3, boo.getNamAut());
staPre.setInt(4, boo.getIdTypBoo());
staPre.setAsciiStream(5, texInpStr, context.length());
int marRes = staPre.executeUpdate();
// Close
utiDb.clsConFun(staPre, con);
// Return
return marRes;
}
// Display book info
private static void disBooFun(int id) throws Exception{
// String command sql, connection, prepared statement
Connection con = utiDb.getConFun();
String strComSql = "select * from t_book2 where id=?";
PreparedStatement staPre = con.prepareStatement(strComSql);
staPre.setInt(1, id);
// Prepared statement execute, get information
ResultSet resSet = staPre.executeQuery();
// Display
if (resSet.next()){
String namBoo = resSet.getString("bookName");
Float priBoo = resSet.getFloat("price");
String namAut = resSet.getString("author");
int idTypBoo = resSet.getInt("bookTypeId");
Clob clo = resSet.getClob("context");
String context = clo.getSubString(1, (int)clo.length());
System.out.println("Book ID: "+id);
System.out.println("Book name: " +namBoo);
System.out.println("Book price: "+priBoo);
System.out.println("Book author: "+namAut);
System.out.println("Book type ID: "+idTypBoo);
System.out.println("Book context: "+context);
}
// Close
utiDb.clsConFun(staPre, con);
}
// Test main
public static void main(String[] args) throws Exception{
// // Book
// File context = new File("/home/linux_ccmj/Documents/ComputerLearning/Web/峰哥体系/1_一头扎进JDBC视频教程/一头扎进JDBC视频教程源码/6_Chapter/helloWorld.txt");
// Book boo = new Book("暖通空调", 77, "中国建筑协会", 3, context);
// // Insert book
// int marRes = insBooFun(boo);
// if (marRes==1){
// System.out.println("成功插入一组数据!");
// }
// else {
// System.out.println("未插入数据!");
// }
disBooFun(14);
}
}
3. BLOB -- Insert
Class UtiDb
Class Book
package JavaJDBC.model;
import java.io.File;
/**
* Created by linux_ccmj on 16-5-10.
*/
public class Book {
private int idBoo;
private String namBoo;
private float priBoo;
private String namAut;
private int idTypBoo;
private File context;
private File picture;
public Book(int idBoo, String namBoo, float priBoo, String namAut, int idTypBoo) {
this.idBoo = idBoo;
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
}
public Book(String namBoo, float priBoo, String namAut, int idTypBoo) {
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
}
public Book(String namBoo, float priBoo, String namAut, int idTypBoo, File context) {
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
this.context = context;
}
public Book(String namBoo, float priBoo, String namAut, int idTypBoo, File context, File picture) {
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
this.context = context;
this.picture = picture;
}
public int getIdBoo() {
return idBoo;
}
public void setIdBoo(int idBoo) {
this.idBoo = idBoo;
}
public String getNamBoo() {
return namBoo;
}
public void setNamBoo(String namBoo) {
this.namBoo = namBoo;
}
public float getPriBoo() {
return priBoo;
}
public void setPriBoo(float priBoo) {
this.priBoo = priBoo;
}
public String getNamAut() {
return namAut;
}
public void setNamAut(String namAut) {
this.namAut = namAut;
}
public int getIdTypBoo() {
return idTypBoo;
}
public void setIdTypBoo(int idTypBoo) {
this.idTypBoo = idTypBoo;
}
public File getContext() {
return context;
}
public void setContext(File context) {
this.context = context;
}
public File getPicture() {
return picture;
}
public void setPicture(File picture) {
this.picture = picture;
}
@Override
public String toString() {
return "[ "+this.idBoo+", "+this.namBoo+", "+this.priBoo+", "+this.namAut+", "+this.idTypBoo+" ]";
}
}
Class BLOB
package JavaJDBC.Chap06.sec02;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* Created by linux_ccmj on 16-5-16.
*/
public class BOLB {
// utiDb
private static UtiDb utiDb = new UtiDb();
// Insert fun
/**
* Insert book into t_book2
* @param boo
* @return
* @throws Exception
*/
private static int insBooFun(Book boo) throws Exception{
// Connection, string sql command, prepared statement
String strComSql = "insert into t_book2 values(null, ?, ?, ?, ?, ?, ?)";
Connection con = utiDb.getConFun();
PreparedStatement staPre = con.prepareStatement(strComSql);
// Prepared statement set value, file ...
File context = boo.getContext();
InputStream texInpStr = new FileInputStream(context);
File picture = boo.getPicture();
InputStream picInpStr = new FileInputStream(picture);
staPre.setString(1, boo.getNamBoo());
staPre.setFloat(2, boo.getPriBoo());
staPre.setString(3, boo.getNamAut());
staPre.setInt(4, boo.getIdTypBoo());
staPre.setAsciiStream(5, texInpStr, context.length());
staPre.setBinaryStream(6, picInpStr, picture.length());
int marRes = staPre.executeUpdate();
// Close
utiDb.clsConFun(staPre, con);
// Return
return marRes;
}
// Display book info
private static void disBooFun(int id) throws Exception{
// String command sql, connection, prepared statement
Connection con = utiDb.getConFun();
String strComSql = "select * from t_book2 where id=?";
PreparedStatement staPre = con.prepareStatement(strComSql);
staPre.setInt(1, id);
// Prepared statement execute, get information
ResultSet resSet = staPre.executeQuery();
// Display
if (resSet.next()){
String namBoo = resSet.getString("bookName");
Float priBoo = resSet.getFloat("price");
String namAut = resSet.getString("author");
int idTypBoo = resSet.getInt("bookTypeId");
Clob clo = resSet.getClob("context");
String context = clo.getSubString(1, (int)clo.length());
System.out.println("Book ID: "+id);
System.out.println("Book name: " +namBoo);
System.out.println("Book price: "+priBoo);
System.out.println("Book author: "+namAut);
System.out.println("Book type ID: "+idTypBoo);
System.out.println("Book context: "+context);
}
// Close
utiDb.clsConFun(staPre, con);
}
// Test main
public static void main(String[] args) throws Exception{
// Book
File context = new File("/home/linux_ccmj/Documents/ComputerLearning/Web/峰哥体系/1_一头扎进JDBC视频教程/一头扎进JDBC视频教程源码/6_Chapter/helloWorld.txt");
File picture = new File("/home/linux_ccmj/Documents/ComputerLearning/Web/峰哥体系/1_一头扎进JDBC视频教程/一头扎进JDBC视频教程源码/6_Chapter/pic1.jpg");
Book boo = new Book("暖通空调", 77, "中国建筑协会", 3, context, picture);
// Insert book
int marRes = insBooFun(boo);
if (marRes==1){
System.out.println("成功插入一组数据!");
}
else {
System.out.println("未插入数据!");
}
// disBooFun(14);
}
}
4. BLOB -- Get Info
Class UtiDb
Class Book
Class BLOB
package JavaJDBC.Chap06.sec02;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
/**
* Created by linux_ccmj on 16-5-16.
*/
public class BOLB {
// utiDb
private static UtiDb utiDb = new UtiDb();
// Insert fun
/**
* Insert book into t_book2
* @param boo
* @return
* @throws Exception
*/
private static int insBooFun(Book boo) throws Exception{
// Connection, string sql command, prepared statement
String strComSql = "insert into t_book2 values(null, ?, ?, ?, ?, ?, ?)";
Connection con = utiDb.getConFun();
PreparedStatement staPre = con.prepareStatement(strComSql);
// Prepared statement set value, file ...
File context = boo.getContext();
InputStream texInpStr = new FileInputStream(context);
File picture = boo.getPicture();
InputStream picInpStr = new FileInputStream(picture);
staPre.setString(1, boo.getNamBoo());
staPre.setFloat(2, boo.getPriBoo());
staPre.setString(3, boo.getNamAut());
staPre.setInt(4, boo.getIdTypBoo());
staPre.setAsciiStream(5, texInpStr, context.length());
staPre.setBinaryStream(6, picInpStr, picture.length());
int marRes = staPre.executeUpdate();
// Close
utiDb.clsConFun(staPre, con);
// Return
return marRes;
}
// Display book info
private static void disBooFun(int id) throws Exception{
// String command sql, connection, prepared statement
Connection con = utiDb.getConFun();
String strComSql = "select * from t_book2 where id=?";
PreparedStatement staPre = con.prepareStatement(strComSql);
staPre.setInt(1, id);
// Prepared statement execute, get information
ResultSet resSet = staPre.executeQuery();
// Display
if (resSet.next()){
String namBoo = resSet.getString("bookName");
Float priBoo = resSet.getFloat("price");
String namAut = resSet.getString("author");
int idTypBoo = resSet.getInt("bookTypeId");
Clob clo = resSet.getClob("context");
String context = clo.getSubString(1, (int)clo.length());
Blob blo = resSet.getBlob("picture");
FileOutputStream out = new FileOutputStream(new File("/home/linux_ccmj/Documents/ComputerLearning/Web/峰哥体系" +
"/1_一头扎进JDBC视频教程/一头扎进JDBC视频教程源码/6_Chapter/pic2.jpg"));
out.write(blo.getBytes(1, (int) blo.length()));
out.close();
System.out.println("Book ID: "+id);
System.out.println("Book name: " +namBoo);
System.out.println("Book price: "+priBoo);
System.out.println("Book author: "+namAut);
System.out.println("Book type ID: "+idTypBoo);
System.out.println("Book context: "+context);
}
// Close
utiDb.clsConFun(staPre, con);
}
// Test main
public static void main(String[] args) throws Exception{
// Book
// File context = new File("/home/linux_ccmj/Documents/ComputerLearning/Web/峰哥体系/1_一头扎进JDBC视频教程/一头扎进JDBC视频教程源码/6_Chapter/helloWorld.txt");
// File picture = new File("/home/linux_ccmj/Documents/ComputerLearning/Web/峰哥体系/1_一头扎进JDBC视频教程/一头扎进JDBC视频教程源码/6_Chapter/pic1.jpg");
// Book boo = new Book("暖通空调", 77, "中国建筑协会", 3, context, picture);
// // Insert book
// int marRes = insBooFun(boo);
// if (marRes==1){
// System.out.println("成功插入一组数据!");
// }
// else {
// System.out.println("未插入数据!");
// }
disBooFun(15);
}
}