2 qq 27626739 qq_27626739 于 2015.07.03 20:31 提问

java.sql.SQLException: ORA-01460: 转换请求无法实施或不合理 异常

package jdbc_day3;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SavePic {
static Connection conn = null;
static PreparedStatement Pstmt = null;

public static void main(String[] args) {
    insertPic();
}
public static void insertPic(){
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "java:oracle:thin:@localhost:1521:orcl";
    String sql = "insert into stu_lu(id, name, photo) values (myseq.nextval,?,?)";
    try {
        Class.forName(driver);
        conn = DriverManager.getConnection(url,"system","0000");
        Pstmt = conn.prepareStatement(sql);

        File f = new File("mykfc.jpg");
        FileInputStream fis = new FileInputStream(f);

        Pstmt.setString(1, "peter");
        Pstmt.setBinaryStream(2, fis, (int)f.length());

        int n = Pstmt.executeUpdate();
        System.out.println(n+"条记录被更新");


    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

6个回答

qq_27626739
qq_27626739   2015.07.03 20:34

不知道哪里错了,希望大神解答!

caozhy
caozhy   Ds   Rxr 2015.07.03 21:32
danielinbiti
danielinbiti   Ds   Rxr 2015.07.03 22:05

这是blob字段吧,不能这么写setBinaryStream
参见
http://www.360doc.com/content/11/1008/11/7662927_154272177.shtml

frank_20080215
frank_20080215   2015.07.04 11:47

客户端和服务端字符集不一致。

解决:

step1、通过执行select * from V$NLS_PARAMETERS查看服务端字符集

PARAMETER VALUE
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_CHARACTERSET ZHS32GB18030
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY $
NLS_NCHAR_CHARACTERSET UTF8
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE

step2、通过regedit修改注册表(仅讨论windows版)进行客户端的字符集,位置\HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME0\NLS_LANG

修改为[NLS_LANGUAGE ]_[NLS_ISO_CURRENCY]_[NLS_CHARACTERSET],

如上面的的服务器,应该修改为:AMERICAN_AMERICA.ZHS32GB18030。

OK了。

yuke198907
yuke198907   2015.07.04 15:36

类型不对,转换一下类型

javachenxi
javachenxi   2016.07.21 19:08

Pstmt.setBinaryStream(2, fis, (int)f.length()); 与驱动的版本有关的,不同版本支持的长度不同的。BLOB 预编译复制有用ORACAL BLOB对象。

Csdn user default icon
上传中...
上传图片
插入图片