JDBC从Oracle中取出long字段类型注意事项

使用JDBC连接Oracle数据库,当取出的字段类型中包含long时,请注意以下几点

1.long类型的字段,在取的时候,只能获取一次,如再次获取否则就会报java.sql.SQLException: 流已被关闭,这个错误,代码如下:

package com.hh.frame.dbmg.test.ora;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LongTest {
	@Test
	public void long_test() throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.123:1521:orcl11g", "test", "123456");
			String createSql = "create table long_test1(id int,comments long,age int)";
			String insertSql = "insert into long_test1 values(1,'测试测试测试测试测试测试测试测试',18)";
			String selectSql = "select comments,age,id  from long_test1";
			//建表
			ps=conn.prepareStatement(createSql);
			ps.execute();
			//插入测试数据
			ps = conn.prepareStatement(insertSql);
			ps.execute();
			//执行查询操作
			ps=conn.prepareStatement(selectSql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				//此处获取long类型字段comments
				String comments = rs.getString("comments");
				//此处再次获取long类型字段comments,则会报错
				comments= rs.getString("comments");
				int id = rs.getInt("id");
				int	age =  rs.getInt("age");
				System.out.print("age: " + age);
				System.out.print("id: " + id);
				System.out.print("comments: " + comments);
				ps=conn.prepareStatement("drop table long_test1");
			ps.execute();
			}
		} catch (SQLException e) {
		ps=conn.prepareStatement("drop table long_test1");
			ps.execute();
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

错误如下:

2.如果SQL中包含long字段,在提取的过程中,如果SQL中long字段后的字段在读取long字段之前被读取就会出现流的错误,否则就不会,假设sql语句是 select comments,id,age from long_test1 ,其中comments为long类型,在取出的时候因为id和age是排在comments后面的,所以要在comments之后读取,否则就会报 java.sql.SQLException: 流已被关闭,这个错误。

package com.hh.frame.dbmg.test.ora;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LongTest {
	@Test
	public void long_test() throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.123:1521:orcl11g", "test", "123456");
			String createSql = "create table long_test1(id int,comments long,age int)";
			String insertSql = "insert into long_test1 values(1,'测试测试测试测试测试测试测试测试',18)";
			String selectSql = "select comments,age,id  from long_test1";
			//建表
			ps=conn.prepareStatement(createSql);
			ps.execute();
			//插入测试数据
			ps = conn.prepareStatement(insertSql);
			ps.execute();
			//执行查询操作
			ps=conn.prepareStatement(selectSql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				//此处先获取排在comments字段后面的id字段,会报流的错误
				int id = rs.getInt("id");
				String comments = rs.getString("comments");
				int	age =  rs.getInt("age");
				System.out.print("age: " + age);
				System.out.print("id: " + id);
				System.out.print("comments: " + comments);
			}
			ps=conn.prepareStatement("drop table long_test1");
			ps.execute();
		} catch (SQLException e) {
		ps=conn.prepareStatement("drop table long_test1");
			ps.execute();
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

当查询语句为:select id,age,comments from long_test1的时候,此时取出顺序则没有要求,因为long类型字段comments后面没有值,所以不需要按查询顺序取出。

当查询语句为:select id,comments,photo from long_test1的时候,因为在long字段类型后面只有一个age字段,所以,在取出的时候,age字段必须要最后一个取出。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值