⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preparedstatementunicode.java

📁 weblogic应用全实例
💻 JAVA
字号:
//声明本类包含在包examples.jdbc.informix4中
package examples.jdbc.informix4;
//声明本类要引入的其他包和类
import java.util.Properties;
import java.sql.*;
import java.io.*;

/**
 *
 * 使用多字节编码建立数据库连接。你需要指定正确的URL和编码。
 * 
 * 这个实例插入3个Unicode字符到一个数据库并执行查询命令,读取数据到结果集
 * 数据从getUnicodeStream读进,转换成十六进制的值,最后打印。
 */


public class PreparedStatementUnicode {
  //主方法
  public static void main (String argv[]) 
       throws SQLException {
      //声明驱动
      Driver myDriver = null;
      try {
      	//加载驱动程序
        myDriver = (Driver) Class.forName("weblogic.jdbc.informix4.Driver").newInstance();
      }
      catch (Exception dr){
      	//异常处理
        System.out.println("unable to load driver");
      }  
  
      // 连接到数据库
      Properties props = new Properties();
      props.put("user",     "informix");
      props.put("password", "secret");
      props.put("server",   "myDBHost"); // hostname on which Informix server runs
      props.put("port",     "1493");       // port on which Informix server listens
      props.put("db",       "myDB");  // name of database on Informix server
      props.put("weblogic.codeset", "myCodeset"); // name of codeset

      Connection c =  myDriver.connect("jdbc:weblogic:informix4", props);

      try {
      	//创建声明对象
        Statement ct = c.createStatement();
        //执行SQL语句:创建表
        ct.executeUpdate("CREATE TABLE dbtest (id smallint,tcol TEXT)");
      }
      catch (Exception cte){
        System.out.println("database already created");
      }

      //用一个参数构造预声明
      PreparedStatement ps = 
          c.prepareStatement("insert into dbtest values (10,?)"); 
        
     // 构造一个Unicode输入流
     String s = new String("\u93e1\u68b0\u897f");
     weblogic.jdbcbase.informix4.UnicodeInputStream uis =
       new weblogic.jdbcbase.informix4.UnicodeInputStream(s);
     try {
       ps.setUnicodeStream(1,uis,uis.available());
     } 
     catch (java.io.IOException ioe) {
       System.out.println("IO Exception in setUnicodeStream");
     }
     ps.executeUpdate();
      
     // 查询数据
     Statement stmt = c.createStatement();
     stmt.execute("select * from dbtest");
     ResultSet rs = stmt.getResultSet();

     InputStream uisout;
     while (rs.next()) {
       uisout= rs.getUnicodeStream(2);
       
       //从UnicodeStream流读入数据并以16进制显示
       int i = 0;   
       while (true) {
         try {
           i = uisout.read();
         } catch (IOException e) {
           System.out.println("IO exception reading Unicode stream");
         }
         if (i == -1) 
            break;
         System.out.println("Ox" + toHex(i) + "  ");
        }                
     }
     //关闭数据库
     rs.close();
     stmt.close();
     c.close();
}

  // 转换字符串为16进制
  final static private char hex [] = 
  { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F' };
  //把数字转换成16进制形式的字符串
  private static String toHex(int number) {
    String s = "";
    long v = number & 0xFFFFFFFF;

    for(int i = 0; (number > 0) || (i < 2); i++, number /= 16) {
        s = hex[number % 16] + s; 
    }
    return s;
  }
}











⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -