日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > 數(shù)據(jù)庫 > 數(shù)據(jù)庫應(yīng)用 > 用SQLData讀寫數(shù)據(jù)庫自定義類型

用SQLData讀寫數(shù)據(jù)庫自定義類型

來源:程序員人生   發(fā)布時(shí)間:2015-02-27 08:32:39 閱讀次數(shù):3013次

如何讀寫自定義類型?SQLData是個(gè)很直觀的解決辦法

在oracle使用手冊(cè)上找到了很好的資料

點(diǎn)擊打開鏈接

http://docs.oracle.com/cd/B10501_01/java.920/a96654/oraoot.htm#1039738

還有詳細(xì)例子(例子很好)

點(diǎn)擊打開鏈接

https://docs.oracle.com/cd/F49540_01/DOC/java.815/a64685/samapp4.htm

但原文太長,我精煉提取了關(guān)于SQLData的關(guān)鍵部份以下(大家只能讀英文了),紅色部份是我寫的實(shí)例代碼,我創(chuàng)建了1個(gè)自定義類型MyTime由小時(shí)和分組成,比如13點(diǎn)

55分就是13:55 

create or replace type MyTime as OBJECT(h int, m int); 

Oracle object types provide support for composite data structures in the database.
          JDBC materializes Oracle objects as instances of particular Java classes. Two main 
           steps in using JDBC to access Oracle objects are: 1) creating the Java classes for 
           the Oracle objects, and 2) populating these classes. You have two options:
         1.Let JDBC materialize the object as a STRUCT.
         2.Explicitly specify the mappings between Oracle objects and Java classes.you can define 
         your classes to implement either the JDBC standard java.sql.SQLData interface or the Oracle 
         extension oracle.sql.ORAData interface
         If you want to create custom object classes for your Oracle objects, then you must define
          entries in the type map that specify the custom object classes that the drivers will
           instantiate for the corresponding Oracle objects.     

   Map<String, Class<?> > map = sample.getTypeMap();
       /*  Use the getTypeMap() method of your OracleConnection object to return the connection's
        *  type map object.
          If the type map in the OracleConnection instance has not been initialized, then the first 
          call to getTypeMap() returns an empty map.*/
         map.put("MYTIME", MyTime.class);
         /*Use the type map's put() method to add map entries. The put() method takes two arguments:
          *  a SQL type name string and an instance of a specified Java class that you want to map to.
          myMap.put(sqlTypeName, classObject);
          SQL type names in the type map must be all uppercase, because that is how the Oracle 
          database stores SQL names.
         The sqlTypeName is a string that represents the fully qualified name of the SQL type in 
         the database. The classObject is the Java class object to which you want to map the SQL 
         type. Get the class object with the Class.forName() method, as follows:
          myMap.put(sqlTypeName, Class.forName(className));
           */      
         sample.setTypeMap(map);
      /*   When you finish adding entries to the map, use the OracleConnection object's setTypeMap() 
       * method to overwrite the connection's existing type map. For example:
        oraconn.setTypeMap(newMap);
        In this example, setTypeMap() overwrites the oraconn connection's original map with newMap.
        If you do not provide a type map with an appropriate entry when using a getObject() call,
         then the JDBC driver will materialize an Oracle object as an instance of the
          oracle.sql.STRUCT class.       
        */

      String sql = "insert into train values(?, ?, ?, ?, ?, ?)";//第5個(gè)參數(shù)是MyTime
         PreparedStatement pstmt = sample.prepareStatement(sql);

        /* Use the setObject() method of the prepared statement to bind your Java datatype 
            * object to the prepared statement.            
            pstmt.setObject(1, emp);
            Use the getObject() method to retrieve the employee object. The following code 
            assumes that there is a type map entry to map the Oracle object to Java type Employee:
             Employee emp = (Employee)ocs.getObject(1); 
                     */
            pstmt.setObject(5, mt2);

          下面是MyTime的類定義

           static class MyTime implements SQLData
   {
  /*The SQLData interface defines methods that translate between SQL and Java for Oracle 
   * database objects. 
   * If you create a custom object class that implements SQLData, then you must provide a 
   * readSQL() method and a writeSQL() method, as specified by the SQLData interface.


       The JDBC driver calls your readSQL() method to read a stream of data values from the database
        and populate an instance of your custom object class. Typically, the driver would use this
         method as part of an OracleResultSet object getObject() call.


       Similarly, the JDBC driver calls your writeSQL() method to write a sequence of data values 
       from an instance of your custom object class to a stream that can be written to the database. 
       Typically, the driver would use this method as part of an OraclePreparedStatement object 
       setObject() call.
   * 
   * 
   * */
      public int h;
      public int m;
      private String sqlUdt = "MYTIME";
      public MyTime(int hh, int mm) {
         h = hh;
         m = mm;
      }

      /*The SQLInput implementation is an input stream class, an instance of which must be passed 
       * in to the readSQL() method.
       * Each readXXX() method converts SQL data to Java data and returns it into an output parameter
       *  of the corresponding Java type. For example, readInt() returns an integer.
       * The SQLOutput implementation is an output stream class, an instance of which must be passed 
       * in to the writeSQL() method. SQLOutput includes a writeXXX() method for each of these Java
       *  types. Each writeXXX() method converts Java data to SQL data,
       * taking as input a parameter of the relevant Java type. For example, writeString() would take 
       * as input a string attribute from your Java class.
       * */
      /*You must implement writeSQL() as follows:


       public void writeSQL(SQLOutput stream) throws SQLException


       The writeSQL() method takes as input a SQLOutput stream.


       When your Java application calls setObject(), the JDBC driver creates 
       a SQLOutput stream object and populates it with data from a custom object 
       class instance. When the driver calls writeSQL(), it passes in this stream parameter.


       For each Java datatype that maps to an attribute of the Oracle object, writeSQL() must
        call the appropriate writeXXX() method of the SQLOutput stream that is passed in.


       For example, if you are writing to EMPLOYEE objects that have an employee name as a 
       CHAR variable and an employee number as a NUMBER variable, then you must have a writeString() 
       call and a writeInt() call in your writeSQL() method. These methods must be called according
        to the order in which attributes appear in the SQL definition of the Oracle object type.


       The writeSQL() method then writes the data converted by the writeXXX() methods to the 
       SQLOutput stream so that it can be written to the database once you execute the prepared 
       statement.
       * 
       * 
       * 
       * */
      public void writeSQL(SQLOutput stream)  throws SQLException//Java data to SQL data
      {
         stream.writeInt(h);
         stream.writeInt(m);
      }
      public String getSQLTypeName() throws SQLException {
         return sqlUdt;
      }

     /* You must implement readSQL() as follows:


     public void readSQL(SQLInput stream, String sql_type_name) throws SQLException


     The readSQL() method takes as input a SQLInput stream and a string that indicates 
     the SQL type name of the data (in other words, the name of the Oracle object type, 
     such as EMPLOYEE).


     When your Java application calls getObject(), the JDBC driver creates a SQLInput 
     stream object and populates it with data from the database. The driver can also determine
      the SQL type name of the data when it reads it from the database. When the driver calls
       readSQL(), it passes in these parameters.


     For each Java datatype that maps to an attribute of the Oracle object, readSQL() must 
     call the appropriate readXXX() method of the SQLInput stream that is passed in.


     For example, if you are reading EMPLOYEE objects that have an employee name as a CHAR 
     variable and an employee number as a NUMBER variable, you must have a readString() call
      and a readInt() call in your readSQL() method. JDBC calls these methods according to
       the order in which the attributes appear in the SQL definition of the Oracle object type.


     The readSQL() method takes the data that the readXXX() methods read and convert, and 
     assigns them to the appropriate fields or elements of a custom object class instance.*/
      public void readSQL(SQLInput stream, String typeName)throws SQLException//SQL data to Java data
      {
         sqlUdt = typeName;
         h = stream.readInt();
         m = stream.readInt();
      }
      public String toString() {
         String res = h + ":" + m;
         return res;
      }
   }
}

附上
        

Reading SQLData Objects from a Result Set

This section summarizes the steps to read data from an Oracle object into your Java application when you choose the SQLData implementation for your custom object class.

These steps assume you have already defined the Oracle object type, created the corresponding custom object class, updated the type map to define the mapping between the Oracle object and the Java class, and defined a statement object stmt.

  1. Query the database to read the Oracle object into a JDBC result set.
    ResultSet rs = stmt.executeQuery("SELECT emp_col FROM personnel");

    The PERSONNEL table contains one column, EMP_COL, of SQL type EMP_OBJECT. This SQL type is defined in the type map to map to the Java class Employee.

  2. Use the getObject() method of your result set to populate an instance of your custom object class with data from one row of the result set. The getObject() method returns the user-defined SQLData object because the type map contains an entry for Employee.
    if (rs.next()) Employee emp = (Employee)rs.getObject(1);

    Note that if the type map did not have an entry for the object, then getObject() would return an oracle.sql.STRUCT object. Cast the output to type STRUCT, because the getObject() method signature returns the generic java.lang.Object type.

    if (rs.next()) STRUCT empstruct = (STRUCT)rs.getObject(1);

    The getObject() call triggers readSQL() and readXXX() calls from the SQLData interface, as described above.


    Note:

    If you want to avoid using a type map, then use the getSTRUCT() method. This method always returns a STRUCT object, even if there is a mapping entry in the type map.


  3. If you have get methods in your custom object class, then use them to read data from your object attributes. For example, if EMPLOYEE has an EmpName (employee name) of type CHAR, and an EmpNum (employee number) of type NUMBER, then provide a getEmpName() method that returns a Java String and a getEmpNum() method that returns an integer (int). Then invoke them in your Java application, as follows:
    String empname = emp.getEmpName(); int empnumber = emp.getEmpNum();

    Note:

    Alternatively, fetch data by using a callable statement object, which also has a getObject() method.


Writing Data to an Oracle Object Using a SQLData Implementation

This section describes the steps in writing data to an Oracle object from your Java application when you choose the SQLData implementation for your custom object class.

This description assumes you have already defined the Oracle object type, created the corresponding Java class, and updated the type map to define the mapping between the Oracle object and the Java class.

  1. If you have set methods in your custom object class, then use them to write data from Java variables in your application to attributes of your Java datatype object.
    emp.setEmpName(empname); emp.setEmpNum(empnumber);

    This statement uses the emp object and the empname and empnumber variables assigned in "Reading SQLData Objects from a Result Set".

  2. Prepare a statement that updates an Oracle object in a row of a database table, as appropriate, using the data provided in your Java datatype object.
    PreparedStatement pstmt = conn.prepareStatement ("INSERT INTO PERSONNEL VALUES (?)");

    This assumes conn is your connection object.

  3. Use the setObject() method of the prepared statement to bind your Java datatype object to the prepared statement.
    pstmt.setObject(1, emp);
  4. Execute the statement, which updates the database.
    pstmt.executeUpdate();

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 91年国产在线 | 亚洲欧美一区二区三区国产精品 | 国产成人精品一区二区在线观看 | 久久国产精品免费视频 | 国产精品区二区三区日本 | 欧美另类专区 | 一区二区三区在线播放 | 狠狠操操 | 欧美日一区二区三区 | 免费毛片网 | 国产区在线 | 精品久久久久久国产 | 丁香婷婷综合网 | 亚洲网站在线看 | 成年人视频在线免费观看 | 国产精品久久久久久久久久久不卡 | a毛片| 九色最新网址 | 欧美成人午夜电影 | 亚洲福利网站 | 91一区二区 | 亚洲欧美网站 | 黄色网址在线播放 | 精品伦精品一区二区三区视频 | 日日爱999| 欧美三级视频网站 | 午夜一级 | 国产精品裸体一区二区三区 | 欧美一级毛片免费的视频 | 国产精品一区二区在线看 | 99老色批| 第九色婷婷 | 国产精品久久久久久久久久浪潮 | av电影在线观看网站 | 日韩毛片在线观看 | 日韩精品| 黄色在线观看视频网站 | 欧美午夜影院 | 日本成人中文字幕 | 人人九九 | 亚洲国产精品一区二区久久 |