A problem needing attention
1. When the database field is a blob type, the setbinarystream (int,inputstream,int) method in PreparedStatement must be used;
2. When the database field is longblob type, you must use the setbinarystream (Int,inputstream,long) method in PreparedStatement .
Otherwise, the title error will be thrown:
Exception in thread "main" Java.lang.AbstractMethodError:Method Com/mysql/jdbc/preparedstatement.setcharacterstream (Iljava/io/reader;) V is abstract
At Com.mysql.jdbc.PreparedStatement.setCharacterStream (Preparedstatement.java)
At Getconnection.main (getconnection.java:19)
This error is thrown when an application attempts to invoke an abstract method. This error is typically caught by the compiler, which can only occur at run time if the definition of a class has made incompatible changes since the last compilation of the current execution method.
second, the actual programming operation1. Create a data table first
CREATE TABLE stuinfo (id int (one) not NULL auto_increment,name varchar (TEN) default Null,content Longtext,image,longblob, PRIMARY KEY (ID)) Engine=innodb;
2. Encode and store images and text files
Import Java.sql.*;import java.io.*;p ublic class getconnection{public static void Main (string[] args) {access2database Adb=new access2database (); Connection conn=adb.getconn ();//transaction dealingpreparedstatement Pstam=null; String sql= "INSERT into Stuinfo (name,content,image) values (?,?,?);"; try {pstam=conn.preparestatement (SQL);p stam.setstring (1, "CJC"); File File=new file ("d:/My Documents/documents/javaprj/test/src/test.txt"); InputStream itxt=new fileinputstream (file); BufferedReader br=new BufferedReader (New InputStreamReader (itxt));p Stam.setcharacterstream (2,BR, (int) file.length ( )); File File1=new file ("d:/My Documents/documents/javaprj/test/src/1.jpg"); InputStream isimg=new FileInputStream (file1); Pstam.setbinarystream (3, isimg, (int) file1.length ());p stam.executeupdate (); Br.close (); Itxt.close (); Isimg.close () ;p stam.close (); Conn.close ();} catch (SQLException e) {e.printstacktrace ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}}
Results:
3. Encode and read images and text files
Code
Import Java.sql.*;import java.io.*;p ublic class getconnection{public static void Main (string[] args) {access2database Adb=new access2database (); Connection conn=adb.getconn ();//transaction dealingpreparedstatement Pstam=null; String sql= "SELECT * from Stuinfo where name=?;"; try {pstam=conn.preparestatement (SQL);p stam.setstring (1, "CJC"); ResultSet Reset=pstam.executequery (); while (Reset.next ()) {System.out.println ("Read text Document ..."); BufferedReader br=new BufferedReader (Reset.getcharacterstream (3)); String Str=null;while ((Str=br.readline ())!=null) {System.out.println (str);} System.out.println ("Read text document Ok!"); System.out.println ("Read image file ..."); Bufferedoutputstream bos=new Bufferedoutputstream (New FileOutputStream (New File ("result.jpg")); byte[] Buf=new byte[ 1024]; Bufferedinputstream bis=new Bufferedinputstream (Reset.getbinarystream (4)); int Count=-1;while (Count=bis.read (BUF, 0, 1024x768)) {!=-1) {bos.write (buf, 0, count);} Bos.flush (); System.out.println ("Read image file ok!"); bos.clOSE ();} Reset.close ();p stam.close (); Conn.close ();} catch (SQLException e) {e.printstacktrace ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}}
Java operation MySQL Database access pictures and other types of files