Example usage for java.sql Connection setTypeMap

List of usage examples for java.sql Connection setTypeMap

Introduction

In this page you can find the example usage for java.sql Connection setTypeMap.

Prototype

void setTypeMap(java.util.Map<String, Class<?>> map) throws SQLException;

Source Link

Document

Installs the given TypeMap object as the type map for this Connection object.

Usage

From source file:Employee.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    Map map = conn.getTypeMap();//from ww  w.  j  a  v  a  2s .com
    map.put("EMP_DATA", Class.forName("Employee"));
    conn.setTypeMap(map);

    ResultSet rs = stmt.executeQuery("SELECT * from Emp");

    Employee employee;

    while (rs.next()) {
        int empId = rs.getInt("EmpId");
        employee = (Employee) rs.getObject("Emp_Info");

        System.out.print("Employee Id: " + empId + ", SSN: " + employee.SSN);
        System.out.print(", Name: " + employee.FirstName + " " + employee.LastName);
        System.out.println(
                ", Yearly Salary: $" + employee.Salary + " Monthly Salary: " + employee.calcMonthlySalary());
    }
    conn.close();
}

From source file:TypeMapDemo.java

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

    Properties p = new Properties();
    p.load(new FileInputStream("db.properties"));
    Class c = Class.forName(p.getProperty("db.driver"));
    System.out.println("Loaded driverClass " + c.getName());

    Connection con = DriverManager.getConnection(p.getProperty("db.url"), "student", "student");
    System.out.println("Got Connection " + con);

    Statement s = con.createStatement();
    int ret;//from   w  ww.  java 2 s.c om
    try {
        s.executeUpdate("drop table MR");
        s.executeUpdate("drop type MUSICRECORDING");
    } catch (SQLException andDoNothingWithIt) {
        // Should use "if defined" but not sure it works for UDTs...
    }
    ret = s.executeUpdate("create type MUSICRECORDING as object (" + "   id integer," + "   title varchar(20), "
            + "   artist varchar(20) " + ")");
    System.out.println("Created TYPE! Ret=" + ret);

    ret = s.executeUpdate("create table MR of MUSICRECORDING");
    System.out.println("Created TABLE! Ret=" + ret);

    int nRows = s.executeUpdate("insert into MR values(123, 'Greatest Hits', 'Ian')");
    System.out.println("inserted " + nRows + " rows");

    // Put the data class into the connection's Type Map
    // If the data class were not an inner class,
    // this would likely be done with Class.forName(...);
    Map map = con.getTypeMap();
    map.put("MUSICRECORDING", MusicRecording.class);
    con.setTypeMap(map);

    ResultSet rs = s.executeQuery("select * from MR where id = 123");
    //"select musicrecording(id,artist,title) from mr");
    rs.next();
    for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
        Object o = rs.getObject(i);
        System.out.print(o + "(Type " + o.getClass().getName() + ")\t");
    }
    System.out.println();
}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test
public void testTypeMap() throws Exception {
    Connection conn = new MyProxy();

    try {/*from   w  ww .j  a  va 2s  .co m*/
        try {
            conn.setTypeMap(conn.getTypeMap());
        } catch (SQLException e) {

        }
    } finally {
        JdbcUtil.closeQuietly(conn);
    }
}

From source file:com.jedi.oracle.OracleCall.java

@Override
public void execute(Connection connection) throws Exception {
    if (connection == null) {
        DataSource dataSource = DataSourceManager.getInstance().getDataSource();
        if (dataSource == null) {
            throw new RuntimeException("Datasource is null");
        }/*  ww  w  . j a va 2 s  .c o  m*/

        connection = dataSource.getConnection();
    }

    fillParametersFromFields();

    String sql = this.createSQL(this.getName());
    Map customTypes = this.getTypeMap();
    if (customTypes != null && !customTypes.isEmpty()) {
        Map map = connection.getTypeMap();
        map.putAll(customTypes);
        connection.setTypeMap(map);
    }

    OracleCallableStatement statement = (OracleCallableStatement) connection.prepareCall(sql);
    OracleParameterUtils.register(statement, this.parameters);
    try {
        statement.execute();
    } catch (SQLException e) {
        if (reExecutionRequired(e)) {
            statement.execute();
        } else {
            throw e;
        }
    }

    OracleParameterUtils.bind(this.parameters, statement);
    fillFieldValuesFromParameters();
}