FileDocCategorySizeDatePackage
ConcatenatingStringsForIUD.javaAPI DocExample3644Thu Jun 14 17:31:40 BST 2001None

ConcatenatingStringsForIUD

public class ConcatenatingStringsForIUD extends Object

Fields Summary
Connection
conn
Constructors Summary
public ConcatenatingStringsForIUD()

    try {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      conn = DriverManager.getConnection(
       "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
    }
    catch (SQLException e) {
     System.err.println(e.getMessage());
     e.printStackTrace();
    }
  
Methods Summary
public voidexecuteIUD(java.lang.String sql)

    int       rslt = 0;
    Statement stmt = null;
    System.out.println(sql);
    try {
      stmt = conn.createStatement();
      rslt = stmt.executeUpdate(sql);
      System.out.println(Integer.toString(rslt) + " rows affected");
      System.out.println(" ");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (stmt != null) 
      try { stmt.close(); } catch (SQLException ignore) { }
    }
  
protected voidfinalize()

    if (conn != null) 
      { try { conn.close(); } catch (SQLException ignore) { } }
    super.finalize();
  
private java.lang.StringformatWithOracleDate(java.sql.Date date)

    if (date != null) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      return "to_date('" + sdf.format(date) + "','YYYY-MM-DD HH24:MI:SS')";
    }
    else {
      return "NULL";
    }
  
private java.lang.StringformatWithSql92Date(java.sql.Date date)

    if (date != null) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      return "{ts '" + sdf.format(date) + "'}";
    }
    else {
      return "NULL";
    }
  
private java.lang.StringformatWithTicks(java.lang.String string)

    if (string != null) {
      char[]       in  = string.toCharArray();
      StringBuffer out = new StringBuffer((int)(in.length * 1.1));
      if (in.length > 0)
        out.append("'");
      for (int i=0;i < in.length;i++) {
        out.append(in[i]);
        if (in[i] == '\'") 
          out.append(in[i]);
      }
      if (in.length > 0)
        out.append("'");
      return out.toString();
    }
    else {
      return "NULL";
    }
  
public static voidmain(java.lang.String[] args)

    ConcatenatingStringsForIUD iud = new ConcatenatingStringsForIUD();

    String last_name           = "O'Reilly";
    String first_name          = "Tim";
    String middle_name         = null;
    Date   birth_date          = Date.valueOf("1971-03-17");
    String mothers_maiden_name = "Oh! I don't know!";

    iud.executeIUD(
     "delete PERSON " + 
     "where  last_name = " + 
     iud.formatWithTicks(last_name) + " " +
     "and    first_name = " + 
     iud.formatWithTicks(first_name));

    iud.executeIUD(
     "insert into PERSON " + 
     "(person_id, last_name, first_name, middle_name, " + 
     "birth_date, mothers_maiden_name) values " +
     "(person_id.nextval, " + 
     iud.formatWithTicks(last_name) + ", " + 
     iud.formatWithTicks(first_name) + ", " + 
     iud.formatWithTicks(middle_name) + ", " + 
     iud.formatWithOracleDate(birth_date) + ", " + 
     iud.formatWithTicks(mothers_maiden_name) + ")");

    birth_date = Date.valueOf("1972-03-17");

    iud.executeIUD(
     "update PERSON " + 
     "set    birth_date = " + 
     iud.formatWithSql92Date(birth_date) + " " +
     "where  last_name = " + 
     iud.formatWithTicks(last_name) + " " +
     "and    first_name = " + 
     iud.formatWithTicks(first_name));