Oracle's CLOB type data processing

Oracle rookie grant authorization: http://www.2cto.com/database/201408/322396.html
oracle View the tablespace where the user is located: http://blog.csdn.net/yitian20000/article/details/6256716
oracle grant Detailed explanation: http://blog.csdn.net/xiayang05/article/details/5861846
The table column in Oracle is changed from VARCHAR2 type to CLOB: http://blog.csdn.net/jssg_tzw/article/details/40829867
Create table test



Test main class
public class testClob {
	public static void main(String[] args){
		testOracleConnection();
	}
	public static String ClobToString(CLOB clob) throws SQLException, IOException {

        String reString = "";
        Reader is = clob.getCharacterStream();// Get the stream
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        // Execute the loop to take out all the strings and pay them to StringBuffer from StringBuffer to STRING
        while (s != null) {
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }
	public static void testOracleConnection()
	{
	    Connection con = null;// Create a database connection
	    PreparedStatement preStm = null;// Create a precompiled statement object, generally use this instead of Statement
	    ResultSet result = null;// Create a result set object
	    try
	    {
	        Class.forName("oracle.jdbc.driver.OracleDriver");// Load Oracle driver
	        System.out.println("Start trying to connect to the database!");
	        //Connecting to Oracle database provides two ways, OCI way and thin way,
	        //OCI method is to communicate with Oracle through the local dynamic connection library,
	        //The speed and security are better, the thin way is to access Oracle remotely
	        // 127.0.0.1 is the local address, efss is the default database name of the simplified version of Oracle
	        String url = "jdbc:oracle:thin:@192.168.126.128:1521:XE";
	        String user = "donald";//User name, the system default account name
	        String password = "123456";//The password you set during installation
	        long startTime = System.currentTimeMillis();
	        con = DriverManager.getConnection(url, user, password);// Get the connection
	        con.setAutoCommit(false);
	        preStm = con.prepareStatement("insert into test values(?,?)");
	        preStm.setInt (1, 1);
	        String name = "jamel";
	        Reader clobReader = new StringReader(name); // Convert text to stream
	        preStm.setCharacterStream(2, clobReader, name.length());// Replace the ? in the sql statement
	        preStm.execute();
	        con.commit ();
	        
	        preStm = con.prepareStatement("select name from test where id = ?");
	        preStm.setInt (1, 1);
	        preStm.execute();
	        con.commit ();
	        result = preStm.getResultSet ();
	        while(result.next()){
	        	CLOB clob = (oracle.sql.CLOB) result.getClob(1);// Get the CLOB field str
	            // Note: The data cannot be obtained with rs.getString("str"), the return is NULL;
	            String content = ClobToString(clob);
	            System.out.println("name:"+ content);
	        }
	        long endTime = System.currentTimeMillis();
	        System.out.println("============time:"+ (endTime-startTime));
	        System.out.println("============hashCode:"+ con.hashCode());
	        if(!con.isClosed()){
	        	 System.out.println("============Connection succeeded!");
	        }
	    }
	    catch (Exception e)
	    {
	    	System.out.println("============Connection failed: "+e.getMessage());
	    	e.printStackTrace ();
	    }
	    finally
	    {
	        try
	        {
	            // Close the above objects one by one, because not closing them will affect performance and occupy resources
	            // Note the order of closing, the last used closes first
	            if (result != null)
	                result.close();
	            if (preStm != null)
	            	preStm.close();
	            if (con != null)
	                con.close();
	            System.out.println("Database connection closed!");
	        }
	        catch (Exception e)
	        {
	            e.printStackTrace ();
	        }
	    }
	
}
}
Console output:

Start trying to connect to the database!
name:jamel
===========time:191
===========hashCode:28890871
===========Connection succeeded!
Database connection closed! Problem: ORA-00942: The table or view does not exist. If you use the navicat client to create a table, the table name will be quoted by default. For example, when you create a table test, you will actually add quotation marks "test" to the table or create a table with navicat. Add quotation marks to the indication: create table "test" If the quotation marks do not work, check whether it is a problem with the user tablespace: check the user tablespace select username, default_tablespace from user_users; change the user default tablespace: alter user donald default tablespace users; summary: When oracle creates a table, it is best not to use the client, use sqlplus, and run the script to create the table.




















Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326976209&siteId=291194637