Types.VARBINARY : Types « java.sql « Java by API

Java by API
1. com.sun.image.codec.jpeg
2. java.applet
3. java.awt
4. java.awt.datatransfer
5. java.awt.dnd
6. java.awt.event
7. java.awt.font
8. java.awt.geom
9. java.awt.im.spi
10. java.awt.image
11. java.awt.print
12. java.beans
13. java.beans.beancontext
14. java.io
15. java.lang
16. java.lang.annotation
17. java.lang.instrument
18. java.lang.management
19. java.lang.ref
20. java.lang.reflect
21. java.math
22. java.net
23. java.nio
24. java.nio.channels
25. java.nio.charset
26. java.rmi.dgc
27. java.rmi.server
28. java.security
29. java.security.cert
30. java.security.spec
31. java.sql
32. java.text
33. java.text.spi
34. java.util
35. java.util.concurrent
36. java.util.concurrent.atomic
37. java.util.concurrent.locks
38. java.util.jar
39. java.util.logging
40. java.util.prefs
41. java.util.regex
42. java.util.spi
43. java.util.zip
44. javax.accessibility
45. javax.activation
46. javax.annotation.security
47. javax.comm
48. javax.crypto
49. javax.crypto.spec
50. javax.ejb
51. javax.imageio
52. javax.imageio.event
53. javax.jws
54. javax.mail
55. javax.mail.internet
56. javax.media.jai
57. javax.microedition.io
58. javax.microedition.lcdui
59. javax.microedition.media
60. javax.microedition.media.control
61. javax.microedition.midlet
62. javax.microedition.pim
63. javax.microedition.rms
64. javax.naming
65. javax.naming.directory
66. javax.naming.event
67. javax.naming.ldap
68. javax.naming.spi
69. javax.net
70. javax.net.ssl
71. javax.persistence
72. javax.print
73. javax.print.attribute
74. javax.print.attribute.standard
75. javax.print.event
76. javax.script
77. javax.servlet
78. javax.servlet.http
79. javax.sound.midi
80. javax.sound.sampled
81. javax.sql
82. javax.sql.rowset
83. javax.swing
84. javax.swing.border
85. javax.swing.colorchooser
86. javax.swing.event
87. javax.swing.filechooser
88. javax.swing.plaf.basic
89. javax.swing.plaf.metal
90. javax.swing.plaf.synth
91. javax.swing.table
92. javax.swing.text
93. javax.swing.text.html
94. javax.swing.text.html.parser
95. javax.swing.text.rtf
96. javax.swing.tree
97. javax.swing.undo
98. javax.tools
99. javax.transaction
100. javax.xml
101. javax.xml.bind
102. javax.xml.bind.annotation
103. javax.xml.crypto.dsig
104. javax.xml.crypto.dsig.keyinfo
105. javax.xml.namespace
106. javax.xml.parsers
107. javax.xml.soap
108. javax.xml.stream
109. javax.xml.stream.events
110. javax.xml.transform
111. javax.xml.transform.dom
112. javax.xml.transform.stream
113. javax.xml.validation
114. javax.xml.ws
115. javax.xml.xpath
116. junit.extensions
117. junit.framework
118. junit.textui
119. org.apache.commons.lang
120. org.apache.commons.lang.builder
121. org.apache.commons.lang.exception
122. org.apache.commons.lang.time
123. org.apache.commons.logging
124. org.apache.commons.math
125. org.eclipse.jface.action
126. org.eclipse.jface.dialogs
127. org.eclipse.jface.operation
128. org.eclipse.jface.viewers
129. org.eclipse.jface.window
130. org.eclipse.jface.wizard
131. org.eclipse.swt
132. org.eclipse.swt.browser
133. org.eclipse.swt.custom
134. org.eclipse.swt.dnd
135. org.eclipse.swt.events
136. org.eclipse.swt.graphics
137. org.eclipse.swt.layout
138. org.eclipse.swt.ole.win32
139. org.eclipse.swt.printing
140. org.eclipse.swt.program
141. org.eclipse.swt.widgets
142. org.junit
143. org.w3c.dom
144. org.xml.sax
145. org.xml.sax.helpers
146. sun.audio
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java by API » java.sql » Types 
Types.VARBINARY
 
/*
 Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
 Use of this software is authorized pursuant to the terms of the license found at
 http://developer.java.sun.com/berkeley_license.html.

 Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.  
 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met: 

 - Redistribution of source code must retain the above copyright notice, 
 this list of conditions and the following disclaimer.

 - Redistribution in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.

 Neither the name of Sun Microsystems, Inc. or the names of contributors may 
 be used to endorse or promote products derived from this software without
 specific prior written permission.

 This software is provided "AS IS," without a warranty of any kind.  
 ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICORSYSTEMS, INC. ("SUN")
 AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN
 IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

 You acknowledge that this software is not designed, licensed or intended for
 use in the design, construction, operation or maintenance of any nuclear
 facility.


 */

/*
 * Copyright 2003 Sun Microsystems, Inc.  ALL RIGHTS RESERVED.
 * Use of this software is authorized pursuant to the terms of the license found at
 * http://developer.java.sun.com/berkeley_license.html.
 */

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Vector;

public class Main {

  public static void main(String[] args) {
    String url = "jdbc:mySubprotocol:myDataSource";

    Connection con;
    Statement stmt;
    try {
      Class.forName("myDriver.ClassName");

    catch (java.lang.ClassNotFoundException e) {
      System.err.print("ClassNotFoundException: ");
      System.err.println(e.getMessage());
    }

    try {
      con = DriverManager.getConnection(url, "myLogin""myPassword");

      stmt = con.createStatement();

      Vector dataTypes = getDataTypes(con);

      String tableName;
      String columnName;
      String sqlType;
      String prompt = "Enter the new table name and hit Return: ";
      tableName = getInput(prompt);
      String createTableString = "create table " + tableName + " (";

      String commaAndSpace = ", ";
      boolean firstTime = true;
      while (true) {
        System.out.println("");
        prompt = "Enter a column name " "(or nothing when finished) \nand hit Return: ";
        columnName = getInput(prompt);
        if (firstTime) {
          if (columnName.length() == 0) {
            System.out.print("Need at least one column;");
            System.out.println(" please try again");
            continue;
          else {
            createTableString += columnName + " ";
            firstTime = false;
          }
        else if (columnName.length() == 0) {
          break;
        else {
          createTableString += commaAndSpace + columnName + " ";
        }

        String localTypeName = null;
        String paramString = "";
        while (true) {
          System.out.println("");
          System.out.println("LIST OF TYPES YOU MAY USE:  ");
          boolean firstPrinted = true;
          int length = 0;
          for (int i = 0; i < dataTypes.size(); i++) {
            DataType dataType = (DataTypedataTypes.get(i);
            if (!dataType.needsToBeSet()) {
              if (!firstPrinted)
                System.out.print(commaAndSpace);
              else
                firstPrinted = false;
              System.out.print(dataType.getSQLType());
              length += dataType.getSQLType().length();
              if (length > 50) {
                System.out.println("");
                length = 0;
                firstPrinted = true;
              }
            }
          }
          System.out.println("");

          int index;
          prompt = "Enter a column type " "from the list and hit Return:  ";
          sqlType = getInput(prompt);
          for (index = 0; index < dataTypes.size(); index++) {
            DataType dataType = (DataTypedataTypes.get(index);
            if (dataType.getSQLType().equalsIgnoreCase(sqlType&& !dataType.needsToBeSet()) {
              break;
            }
          }

          localTypeName = null;
          paramString = "";
          if (index < dataTypes.size()) { // there was a match
            String params;
            DataType dataType = (DataTypedataTypes.get(index);
            params = dataType.getParams();
            localTypeName = dataType.getLocalType();
            if (params != null) {
              prompt = "Enter " + params + ":  ";
              paramString = "(" + getInput(prompt")";
            }
            break;
          else // use the name as given
            prompt = "Are you sure?  " "Enter 'y' or 'n' and hit Return:  ";
            String check = getInput(prompt" ";
            check = check.toLowerCase().substring(01);
            if (check.equals("n"))
              continue;
            else {
              localTypeName = sqlType;
              break;
            }
          }
        }

        createTableString += localTypeName + paramString;

      }

      createTableString += ")";
      System.out.println("");
      System.out.print("Your CREATE TABLE statement as ");
      System.out.println("sent to your DBMS:  ");
      System.out.println(createTableString);
      System.out.println("");

      stmt.executeUpdate(createTableString);

      stmt.close();
      con.close();

    catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
  }

  private static Vector getDataTypes(Connection conthrows SQLException {
    String structName = null, distinctName = null, javaName = null;

    // create a vector of class DataType initialized with
    // the SQL code, the SQL type name, and two null entries
    // for the local type name and the creation parameter(s)

    Vector dataTypes = new Vector();
    dataTypes.add(new DataType(java.sql.Types.BIT, "BIT"));
    dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT"));
    dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT"));
    dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER"));
    dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT"));
    dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT"));
    dataTypes.add(new DataType(java.sql.Types.REAL, "REAL"));
    dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE"));
    dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC"));
    dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL"));
    dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR"));
    dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR"));
    dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR"));
    dataTypes.add(new DataType(java.sql.Types.DATE, "DATE"));
    dataTypes.add(new DataType(java.sql.Types.TIME, "TIME"));
    dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP"));
    dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY"));
    dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY"));
    dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY"));
    dataTypes.add(new DataType(java.sql.Types.NULL, "NULL"));
    dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER"));
    dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB"));
    dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB"));

    DatabaseMetaData dbmd = con.getMetaData();
    ResultSet rs = dbmd.getTypeInfo();
    while (rs.next()) {
      int codeNumber = rs.getInt("DATA_TYPE");
      String dbmsName = rs.getString("TYPE_NAME");
      String createParams = rs.getString("CREATE_PARAMS");

      if (codeNumber == Types.STRUCT && structName == null)
        structName = dbmsName;
      else if (codeNumber == Types.DISTINCT && distinctName == null)
        distinctName = dbmsName;
      else if (codeNumber == Types.JAVA_OBJECT && javaName == null)
        javaName = dbmsName;
      else {
        for (int i = 0; i < dataTypes.size(); i++) {
          // find entry that matches the SQL code,
          // and if local type and params are not already set,
          // set them
          DataType type = (DataTypedataTypes.get(i);
          if (type.getCode() == codeNumber) {
            type.setLocalTypeAndParams(dbmsName, createParams);
          }
        }
      }
    }

    int[] types = Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT };
    rs = dbmd.getUDTs(null, "%""%", types);
    while (rs.next()) {
      String typeName = null;
      DataType dataType = null;

      if (dbmd.isCatalogAtStart())
        typeName = rs.getString(1+ dbmd.getCatalogSeparator() + rs.getString(2"."
            + rs.getString(3);
      else
        typeName = rs.getString(2"." + rs.getString(3+ dbmd.getCatalogSeparator()
            + rs.getString(1);

      switch (rs.getInt(5)) {
      case Types.STRUCT:
        dataType = new DataType(Types.STRUCT, typeName);
        dataType.setLocalTypeAndParams(structName, null);
        break;
      case Types.DISTINCT:
        dataType = new DataType(Types.DISTINCT, typeName);
        dataType.setLocalTypeAndParams(distinctName, null);
        break;
      case Types.JAVA_OBJECT:
        dataType = new DataType(Types.JAVA_OBJECT, typeName);
        dataType.setLocalTypeAndParams(javaName, null);
        break;
      }
      dataTypes.add(dataType);
    }

    return dataTypes;
  }

  private static String getInput(String promptthrows SQLException {

    System.out.print(prompt);
    System.out.flush();

    try {
      java.io.BufferedReader bin;
      bin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

      String result = bin.readLine();
      return result;

    catch (java.io.IOException ex) {
      System.out.println("Caught java.io.IOException:");
      System.out.println(ex.getMessage());
      return "";
    }
  }
}

class DataType {

  private int code;

  private String SQLType;

  private String localType = null;

  private String params = null;

  private boolean needsSetting = true;

  public DataType(int code, String SQLType) {
    this.code = code;
    this.SQLType = SQLType;

  }

  public boolean needsToBeSet() {
    return needsSetting;
  }

  public int getCode() {
    return code;
  }

  public String getSQLType() {
    return SQLType;
  }

  public String getLocalType() {
    return localType;
  }

  public String getParams() {
    return params;
  }

  public void setLocalTypeAndParams(String local, String p) {
    if (needsSetting) {
      localType = local;
      params = p;
      needsSetting = false;
    }
  }
}

   
  
Related examples in the same category
1. Types.BIGINT
2. Types.BIT
3. Types.BLOB
4. Types.CLOB
5. Types.DECIMAL
6. Types.DATE
7. Types.DOUBLE
8. Types.FLOAT
9. Types.INTEGER
10. Types.LONGVARCHAR
11. Types.LONGVARBINARY
12. Types.NULL
13. Types.TIMESTAMP
14. Types.VARCHAR
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.