Witam. Tworzę projekt na studia z obiektowych baz danych. Niestety utknąłem na początku, podczas wstawiania wierszy. Korzystam z Oracla w wersji 10.2.0.1.0 i takiego też sterownika(na wersję nie mam wpływu). Poniżej przedstawiam kod:
package oracleobjects;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.sql.*;
import oracle.sql.StructDescriptor;
public class OracleObjects
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Driver?");
e.printStackTrace();
return;
}
System.out.println("Driver Registered!");
Connection connection = null;
try
{
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@xxx", "xxx",
"xxx");
}
catch (SQLException e)
{
System.out.println("Connection Failed!");
e.printStackTrace();
return;
}
if (connection != null)
{
System.out.println("Connected");
}
else
{
System.out.println("Failed to make connection!");
}
StructDescriptor structDescriptor = null;
try
{
structDescriptor = StructDescriptor.createDescriptor("OSOBA_TYP", connection);
}
catch (SQLException ex)
{
System.out.println("Descriptor?");
}
Object[] attributes = new Object[3];
attributes[0] = new BigDecimal(1234); //id
attributes[1] = "Rafał"; //imię
attributes[2] = "Kowalski"; //nazwisko
STRUCT struct = null;
try
{
struct = new STRUCT(structDescriptor, connection, attributes);
}
catch (SQLException ex)
{
System.out.println("Struct?");
}
int rows = 0;
try
{
PreparedStatement preparedStatement
= connection.prepareStatement("insert into OSOBA_TAB values ( ? )");
preparedStatement.setObject(1, struct, Types.STRUCT);
rows = preparedStatement.executeUpdate();
}
catch (SQLException ex)
{
System.out.println("Statement?");
ex.printStackTrace();
}
System.out.println(rows);
try
{
connection.close();
}
catch (SQLException ex)
{
Logger.getLogger(OracleObjects.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Kolumny w bazie ustawione mam na not null. Po uruchomieniu programu dostaję błąd:
java.sql.SQLException: ORA-01400: nie można wstawić wartości NULL do ("xxx"."OSOBA_TAB"."IMIE")
Po usunięciu tych ograniczeń wiersz jest wstawiany z poprawną wartością BigDecimal, ale zamiast stringów są nulle. W debugerze sprawdziłem wartości datumArray w structcie i stringi mapowane są na char, lecz nie są ustawiane ich bytowe wartości. W bazie ustawione są jako varchary2. Może to jest problem? Bardzo proszę o pomoc.