package cn.jn.bq.mawt.test;
import java.io.*;
import java.sql.*;
/*
* DB: bq_test
* Table: tsblob
* create table tsblob(
* tid bigint not null auto_increment primary key,
* tname varchar(20) not null,
* tcontent text
* );
*/
public class DBBlob {
private String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/bq_test";
private String user = "root";
private String pass = "123456";
public DBBlob(){
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DBBlob db = new DBBlob();
//db.insertCBlob();
db.queryCBlob();
}
//Statement stmt = conn.createStatement();
//ResultSet rs = stmt.executeQuery("select tname,ulevel,tfile from template");
public Connection getConn()
{
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,pass);
return conn;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
*
*/
private void insertCBlob() {
BufferedInputStream in = null;
BufferedReader br = null;
Connection con = getConn();
File text1 = new File("/home/grid/taobao/files/template.xml");
try {
con.setAutoCommit(false);
//in = new BufferedInputStream(new FileInputStream(image));
// 用下面的方法可以获得文件中的中文
// br=new BufferedReader(new InputStreamReader(new
// FileInputStream(text1)));
// br=new BufferedReader(new InputStreamReader(new
// FileInputStream(text1),"UTF-8"));
br = new BufferedReader(new FileReader(text1));
Statement stmt = con.createStatement();
String insql = "insert into tsblob(tname,tcontent) values(?,?)";
PreparedStatement ps=con.prepareStatement(insql);
ps.setString(1, "marvin");
ps.setCharacterStream(2, br, (int)text1.length());
// br=new BufferedInputStream(new FileInputStream(text1));
// ps = con.prepareStatement("insert into photostu(id,name,photo,descr) values(null,?,?,?)");
// ps.setString(1, "liu1");
// ps.setBinaryStream(2, in, (int) image.length());
// System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
// ps.setCharacterStream(3, br, (int) text1.length());
// ps.setBinaryStream(3, br, (int)text1.length());
ps.executeUpdate();
con.commit();
System.out.println("插入大数据成功!");
ps.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void queryCBlob()
{
Connection con = getConn();
BufferedWriter bw = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = con.prepareStatement("select tname,tcontent from tsblob");
rs = ps.executeQuery();
if(rs.next()){
System.out.println(rs.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
import java.io.*;
import java.sql.*;
/*
* DB: bq_test
* Table: tsblob
* create table tsblob(
* tid bigint not null auto_increment primary key,
* tname varchar(20) not null,
* tcontent text
* );
*/
public class DBBlob {
private String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/bq_test";
private String user = "root";
private String pass = "123456";
public DBBlob(){
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DBBlob db = new DBBlob();
//db.insertCBlob();
db.queryCBlob();
}
//Statement stmt = conn.createStatement();
//ResultSet rs = stmt.executeQuery("select tname,ulevel,tfile from template");
public Connection getConn()
{
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,pass);
return conn;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
*
*/
private void insertCBlob() {
BufferedInputStream in = null;
BufferedReader br = null;
Connection con = getConn();
File text1 = new File("/home/grid/taobao/files/template.xml");
try {
con.setAutoCommit(false);
//in = new BufferedInputStream(new FileInputStream(image));
// 用下面的方法可以获得文件中的中文
// br=new BufferedReader(new InputStreamReader(new
// FileInputStream(text1)));
// br=new BufferedReader(new InputStreamReader(new
// FileInputStream(text1),"UTF-8"));
br = new BufferedReader(new FileReader(text1));
Statement stmt = con.createStatement();
String insql = "insert into tsblob(tname,tcontent) values(?,?)";
PreparedStatement ps=con.prepareStatement(insql);
ps.setString(1, "marvin");
ps.setCharacterStream(2, br, (int)text1.length());
// br=new BufferedInputStream(new FileInputStream(text1));
// ps = con.prepareStatement("insert into photostu(id,name,photo,descr) values(null,?,?,?)");
// ps.setString(1, "liu1");
// ps.setBinaryStream(2, in, (int) image.length());
// System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
// ps.setCharacterStream(3, br, (int) text1.length());
// ps.setBinaryStream(3, br, (int)text1.length());
ps.executeUpdate();
con.commit();
System.out.println("插入大数据成功!");
ps.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void queryCBlob()
{
Connection con = getConn();
BufferedWriter bw = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = con.prepareStatement("select tname,tcontent from tsblob");
rs = ps.executeQuery();
if(rs.next()){
System.out.println(rs.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}
}
}