jdbc oracle clob

import java.io.BufferedReader;
import java.io.Reader;
import java.io.Writer;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {
	private static Connection getConn() {
		String driver = "oracle.jdbc.driver.OracleDriver";
		// String url = "jdbc:oracle:thin:@127.0.0.1:1521:pdbmyoracle";//
		// 设置连接字符串
		String url = "jdbc:oracle:thin:@//127.0.0.1:1521/pdbmyoracle";
		String username = "abc";// 用户名
		String password = "abc";// 密码
		Connection conn = null; // 创建数据库连接对象
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return conn;
	}

	private static void query() {
		Connection conn = getConn();
		String sql = "select * from a";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			// 建立一个结果集,用来保存查询出来的结果

			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {
				String id = rs.getString("id");
				System.out.println(id);

				Clob clob = rs.getClob("content");
				Reader reader = clob.getCharacterStream();
				BufferedReader br = new BufferedReader(reader);
				StringBuffer sb = new StringBuffer();
				String s = "";
				while ((s = br.readLine()) != null) {
					sb.append(s);
					s = br.readLine();
				}
				System.out.println(sb.toString());
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void callProcedure() {
		try {
			Connection con = getConn();
			CallableStatement cs = con.prepareCall("{call insert_a(?)}");
			Clob clob = con.createClob();
			Writer writer = clob.setCharacterStream(1);
			writer.write("106148,107161,104672,106903,105009,107667,104512,106715,105010,103265,104694,104695,104697,105053,108169,104189");
			writer.close();
			cs.setClob(1, clob);
			boolean b = cs.execute();
			System.out.println(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		callProcedure();
	}

}

 

你可能感兴趣的:(oracle)