Java Read Clob

Source: Internet
Author: User

Java read clobcreationtime--2018 July 1 09:41 Author:marydon

1. Description

The JDBC data type is CLOB, which corresponds to the Java string type.

2. Import

Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJava.sql.Clob;

3. Code implementation

 PublicList<map<string, object>>queryForList () {List<map<string, object>> rowlist =NewArraylist<map<string, object>>(); String Driver= ""; String URL= ""; String username= ""; String Password= ""; String SQL= ""; Log.info ("Query SQL Condition:" +SQL); Try {        //loading drivers, using the method of implicitly registering the driverClass.forName (driver); Log.info ("Load driver succeeded!" "); } Catch(ClassNotFoundException e) {log.error ("Loading the driver, there was an error in the method of implicitly registering the driver here:" +e.getmessage ());    E.printstacktrace (); }    Try {        //Create a Connection objectConnection con =drivermanager.getconnection (URL, username, password); //To create a SQL execution ObjectStatement st =con.createstatement (); //executes the SQL statement and returns the result setResultSet rs =st.executequery (SQL); //Get result set structure information, meta dataResultSetMetaData RSMD =Rs.getmetadata (); //get number of columns        intColumnCount =Rsmd.getcolumncount (); //used to receive each row of datamap<string, object> rowdata =NULL; //CLOB TypeClob Clob =NULL; //Clob turns into a stringString clobstring = "";  while(Rs.next ()) {RowData=NewHashmap<string, object>();  for(inti = 1; I <= ColumnCount; i++) {                //determine if the data type is CLOB                if(Rs.getobject (i)instanceofClob) {//Clob Turn StringCLOB =(Clob) rs.getobject (i); Clobstring= Clob.getsubstring ((Long) 1, (int) clob.length ());                Rowdata.put (Rsmd.getcolumnname (i), clobstring); } Else{rowdata.put (Rsmd.getcolumnname (i), rs.getobject (i));        }} rowlist.add (RowData); }        //close the related object        if(rs! =NULL) {rs.close (); }        if(St! =NULL) {st.close (); }        if(Con! =NULL) {con.close (); }    } Catch(SQLException e) {e.printstacktrace (); }    returnrowlist;}

Related recommendations:
    • Similar articles

Java Read Clob

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.