#java.sql.SQLOutput

# 描述

SQLOutput用于参数绑定过程中把用户自定义的java类型的对象映射成数据库中的UDT类型。

java.sql.SQLOutput是JDBC规范中用于写入数据库自定义类型字段值的核心接口。当实现SQLData接口的Java类执行writeSQL()方法时,JDBC会自动创建SQLOutput实例,通过该实例的写入方法,按顺序将Java对象的属性值写入到数据库自定义类型(如STRUCT、复合类型)的对应字段中。

详细接口说明请参考JDBC官方网站 (opens new window)。

# 方法

java.sql.SQLOutput包含的方法如下:

方法 返回类型
writeString(String) throws SQLException void
writeNString(String) throws SQLException void
writeBoolean(boolean) throws SQLException void
writeByte(byte) throws SQLException void
writeShort(short) throws SQLException void
writeInt(int) throws SQLException void
writeLong(long) throws SQLException void
writeFloat(float) throws SQLException void
writeDouble(double) throws SQLException void
writeBigDecimal(BigDecimal) throws SQLException void
writeBytes(byte[]) throws SQLException void
writeDate(Date) throws SQLException void
writeTime(Time) throws SQLException void
writeTimestamp(TImeStamp) throws SQLException void
writeObject(Object) throws SQLException void
writeObject(Class<T> type) throws SQLException void
writeArray(Array) throws SQLException void
writeRowId(RowID) throws SQLException void
writeBlob(Blob) throws SQLException void
writeClob(Clob) throws SQLException void
writeNClob(NClob) throws SQLException void
writeSQLXML(SQLXML) throws SQLException void
writeCharacterStream(Reader) throws SQLException void
writeAsciiStream(InputStream) throws SQLException void
writeBinaryStream(InputStream) throws SQLException void

# 示例

以下示例假设数据库中已创建EMPLOYEE_TYPE、SKILLS_TYPE和SAVE_EMPLOYEE。其中,SKILLS_TYPE为数据库中定义的VARRAY类型。

package quickstart;

import com.yashandb.jdbc.YasConnection;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.RowId;
import java.sql.SQLData;
import java.sql.SQLInput;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.sql.Struct;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Properties;

public class Employee implements SQLData {
    private int id;
    private String name;
    private String nName;
    private boolean active;
    private byte level;
    private short dept;
    private int age;
    private long empNo;
    private float bonusRate;
    private double salary;
    private BigDecimal bonus;
    private byte[] photo;
    private java.sql.Date hireDate;
    private Time startTime;
    private Timestamp lastLogin;
    private SQLData metadata;
    private Array skills;
    private Blob resume;
    private Clob bio;

    // Getter和Setter
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public static Connection getConnection()
            throws SQLException, ClassNotFoundException {
        String url = "jdbc:yasdb://192.168.1.2:1688/yasdb";
        Properties info = new Properties();
        info.setProperty("user", "username");
        info.setProperty("password", "passwd");
        Class.forName("com.yashandb.jdbc.Driver");
        return DriverManager.getConnection(url, info);
    }

    @Override
    public String getSQLTypeName() throws SQLException {
        return "EMPLOYEE_TYPE";
    }

    @Override
    public void readSQL(SQLInput stream, String typeName) throws SQLException {
        id = stream.readInt();
        name = stream.readString();
        nName = stream.readNString();
        active = stream.readBoolean();
        level = stream.readByte();
        dept = stream.readShort();
        age = stream.readInt();
        empNo = stream.readLong();
        bonusRate = stream.readFloat();
        salary = stream.readDouble();
        bonus = stream.readBigDecimal();
        photo = stream.readBytes();
        hireDate = stream.readDate();
        startTime = stream.readTime();
        lastLogin = stream.readTimestamp();
        metadata = (SQLData) stream.readObject();
        skills = stream.readArray();
        resume = stream.readBlob();
        bio = stream.readClob();
    }

    @Override
    public void writeSQL(SQLOutput stream) throws SQLException {
        // 基础类型
        stream.writeInt(id);
        stream.writeString(name);
        stream.writeNString(nName);
        stream.writeBoolean(active);
        stream.writeByte(level);
        stream.writeShort(dept);
        stream.writeInt(age);
        stream.writeLong(empNo);
        stream.writeFloat(bonusRate);
        stream.writeDouble(salary);

        // 大数值类型
        stream.writeBigDecimal(bonus != null ? bonus : BigDecimal.ZERO);

        // 二进制数据
        stream.writeBytes(photo != null ? photo : new byte[0]);

        // 日期和时间
        stream.writeDate(hireDate);
        stream.writeTime(startTime);
        stream.writeTimestamp(lastLogin != null ? lastLogin : new Timestamp(System.currentTimeMillis()));

        // 对象
        stream.writeObject(metadata);

        // 数组
        stream.writeArray(skills);

        // 大对象
        stream.writeBlob(resume);
        stream.writeClob(bio);
    }

    // 使用方法
    public void saveEmployee(Connection conn) throws SQLException {
        String sql = "{call SAVE_EMPLOYEE(?)}";
        try (CallableStatement cs = conn.prepareCall(sql)) {
            // 创建 Employee 对象
            Employee emp = new Employee();
            emp.id = 1001;
            emp.name = "John Doe";
            emp.nName = "John";
            emp.active = true;
            emp.level = 3;
            emp.dept = 101;
            emp.age = 30;
            emp.empNo = 1234567890L;
            emp.bonusRate = 0.15f;
            emp.salary = 75000.0;
            emp.bonus = new BigDecimal("5000.00");
            emp.photo = new byte[]{0x01, 0x02, 0x03};
            emp.hireDate = java.sql.Date.valueOf("2026-09-02");
            emp.startTime = Time.valueOf("09:00:00");
            emp.lastLogin = new Timestamp(System.currentTimeMillis());
            emp.metadata = null;
            YasConnection yasConnection = (YasConnection) conn;
            emp.skills = yasConnection.createArray("SKILLS_TYPE",
                    new Object[]{"Java", "SQL", "Spring"});
            emp.resume = conn.createBlob();
            emp.resume.setBytes(1, "Resume content".getBytes());
            emp.bio = conn.createClob();
            emp.bio.setString(1, "Employee biography");

            // 创建 STRUCT
            Struct empStruct = conn.createStruct("EMPLOYEE_TYPE",
                    new Object[]{
                            emp.id, emp.name, emp.nName, emp.active, emp.level,
                            emp.dept, emp.age, emp.empNo, emp.bonusRate, emp.salary,
                            emp.bonus, emp.photo, emp.hireDate, emp.startTime,
                            emp.lastLogin, emp.metadata, emp.skills, emp.resume,
                            emp.bio
                    });

            cs.setObject(1, empStruct);
            cs.execute();
        }
    }

    public static void main(String[] args) {
        try (Connection conn = getConnection()) {
            new Employee().saveEmployee(conn);
            System.out.println("Employee saved successfully");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

正常情况下,程序输出如下:

Employee saved successfully
pdf-btn 下载文档 copy-btn 复制链接
edit-icon
反馈
coperate-icon
coperate
合作
communication-icon
communicate
交流