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:
Java Read Clob