Java operation MySQL database access images and other types of files, other types of mysql

Source: Internet
Author: User

Java operation MySQL database access images and other types of files, other types of mysql
1. Notes

1. When the database field isBlobType, must usePreparedStatementInSetBinaryStream (int, InputStream, int)Method;
2. When the database field isLongblobType, must usePreparedStatementInSetBinaryStream (int, InputStream, long)Method.

Otherwise, an error such as the question is 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 call an abstract method. This error is usually captured by the compiler. If the definition of a class changes incompatible after the last compilation of the current execution method, this error may only occur during running.

Ii. Actual programming 1. Create a data table first

Create table stuinfo (id int (11) not null auto_increment, name varchar (10) default NULL, content longText, image, longBlob, primary key (id) ENGINE = InnoDB;

2. encoding and storage of images and text files

Import java. SQL. *; import java. io. *; public 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); pstam. setString (1, "cjc"); File file = new File ("D:/My Documents/JavaPrj/Test/src/test.txt "); inputStream itxt = new FileInputStream (file); BufferedReader br = new BufferedReader (new InputStreamReader (itxt); pstam. setCharacterStream (2, br, (int) file. length (); File file1 = new File ("D:/My Documents/JavaPrj/Test/src/1.jpg"); InputStream isimg = new FileInputStream (file1 ); pstam. setBinaryStream (3, isimg, (int1_file1.length(1_1_pstam.exe cuteUpdate (); br. close (); itxt. close (); isimg. close (); pstam. close (); conn. close ();} catch (SQLException e) {e. printStackTrace ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}
Result:


3. Read images and text files by encoding

Code

import java.sql.*;import java.io.*;public 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);pstam.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, 1024))!=-1){bos.write(buf, 0, count);}bos.flush();System.out.println("Read image file OK!");bos.close();}reset.close();pstam.close();conn.close();} catch (SQLException e) {e.printStackTrace();} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.