在顶目中不识不知中遇见PreparedStatement
在存DB时出现乱码,搅扰了好久毕竟消除难点
 

//把图纸以二进制写入数据库
//三遍性只可以写入或读取鲜明目录下的单个文件
package write_read;

标题代码如下

import java.io.*;
import java.sql.*;

[java]

public class PutImg {
public void putimg() {
  try {
  
   String url =
“jdbc:mysql://localhost/img?useUnicode=true&characterEncoding=gb2312”;
   Class.forName(“com.mysql.jdbc.Driver”);
   String username=”root”;
   String password=”root”;
//   String url = “jdbc:db2://localhost:50000/SJPT”;
//   Class.forName(“com.ibm.db2.jcc.DB2Driver”).newInstance();
//   String username=”DB_SJPT”;
永利皇宫登录网址 ,//   String password=”sjpt”;
   Connection conn =
DriverManager.getConnection(url,username,password);
   System.out.println(“测试连接成功”);
   Statement stmt = conn.createStatement();
   //stmt.execute(“insert   into   imgt (id)   values   (5)”);
  
   PreparedStatement pstmt = null;
   String sql = “”;
  
//   LinkedList<String> folderList = new
LinkedList<String>();
//  folderList.add(“1”);
//   while(folderList.size()>0){
//    File file=new File((String)folderList.poll());
//    File[] files=file.listFiles();
//   List<File> fileList=new ArrayList<File>();
//    for(int i=0;i<files.length;i++){
//    if(files[i].isDirectory())
//    folderList.add(files[i].getPath());
//    else{
//   fileList.add(files[i]);
//    InputStream photoStream = new FileInputStream(fileList.get(i));
//    sql = “INSERT INTO imgtable  (img) VALUES (?)”;
//   
//    pstmt = conn.prepareStatement(sql);
//    pstmt.setBinaryStream(i, photoStream, (int) fileList.size());
//    pstmt.executeUpdate();
//    if(pstmt.executeUpdate()==1){
//    System.out.println(“图片写入成功!”);
//    }else{
//    System.out.println(“图片写入退步!”);  
//    }
//    }
//    }
////    for(Flie f:fileList){
////    f.getAbsoluteFile();
////  
////    }
//   }
   File files = new File(“d:\\blog.jpg”);
   InputStream photoStream = new FileInputStream(files);
   //sql = ”   UPDATE   imgt   SET   img   =   ?   “;
   sql = “INSERT INTO imgtable  (img) VALUES (?)”;
  
   pstmt = conn.prepareStatement(sql);
   pstmt.setBinaryStream(1, photoStream, (int) files.length());
   pstmt.executeUpdate();  
 
   if(pstmt.executeUpdate()==1){
   System.out.println(“图片写入成功!”);
   }else{
   System.out.println(“图片写入失利!”);  
   }
   stmt.close();
   pstmt.close();
   conn.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
}
public static void main(String args[]){
  PutImg pi=new PutImg();
  pi.putimg();
}
}

pstmt = con.prepareStatement(INSERT_OFFLINE);  

//从数据库读取图片
package write_read;

           pstmt.setString(1, username);  

import java.io.*;
import java.sql.*;
public class GetImg {

           pstmt.setLong(2, messageID);  

// private static final String URL=”jdbc:db2://localhost:50000/SJPT”;
// String username=”DB_SJPT”;
// String password=”sjpt”;
private static final String URL=
“jdbc:mysql://localhost/img?useUnicode=true&characterEncoding=gb2312”;
String username=”root”;
String password=”root”;
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;

           pstmt.setString(3, StringUtils.dateToMillis(new
java.util.Date()));  

public void blobRead(String outfile, int picID) throws Exception {
  FileOutputStream fos = null;
  InputStream is = null;
  byte[] Buffer = new byte[4096];
  try {
// Class.forName(“com.ibm.db2.jcc.DB2Driver”);
   Class.forName(“com.mysql.jdbc.Driver”);
   conn = DriverManager.getConnection(URL,username,password);
   System.out.println(“测试连接成功”);
   pstmt = conn.prepareStatement(“select img from imgtable where
id=?”);
   pstmt.setInt(1, picID); // 传入要取的图形的ID
   rs = pstmt.executeQuery();
  
   System.out.println(rs);
   rs.next();
  
  
   file = new File(outfile);
   if (!file.exists()) {
    file.createNewFile(); // 倘诺文件不设有,则创立
   }
   fos = new FileOutputStream(file);
   is = rs.getBinaryStream(“img”);
   System.out.println(“1000000″+is);
   int size = 0;
  
  while ((size = is.read(Buffer)) != -1) {
 
    // System.out.println(size);
    fos.write(Buffer, 0, size);
   }
  } catch (Exception e) {
   System.out.println( e.getMessage());
  } finally {
   // 关闭用到的财富
   fos.close();
   rs.close();
   pstmt.close();
   conn.close();
  }
}
public static void main(String[] args) {
  try {
   GetImg gi=new GetImg();
   gi.blobRead(“E:/getimgs/liyanli.jpg”, 5);
   System.out.println(gi);
   if(gi!=null){
   System.out.println(“图片读取成功”);
   }else{
   System.out.println(“图片读取失利!”);  
   }
  } catch (Exception e) {
   System.out.println(“[Main func error: ]” + e.getMessage());
  }
}
}

           pstmt.setInt(4, msgXML.length());  

           pstmt.setString(5, “粤语内容”);  

           pstmt.executeUpdate();  www.2cto.com    

 

调弄整理时方可观看在   pstmt.executeUpdate();  行 pstmt  中的 “普通话内容”
产生  ????  

网站地图xml地图