Matching Java types and SQL types

Source: Internet
Author: User
Tags date array end sql string stmt table name variable

Example of dynamic table creation

(1)
public 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;
}
}
}

(2)
Import java.sql.*;
public class Sqltypescreate {
public static void Main (String [] args) {
String url= "Jdbc:oracle:thin: @localhost: 1521:oral";
String user= "SYSTEM";
String pass= "Manager";
try {
Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();
catch (Exception e) {System.out.println (e);}
try {
Connection con = drivermanager.getconnection (URL, user,pass);
Statement stmt = Con.createstatement ();
String TableName;
To create a table name variable in a table statement
String ColumnName;
Column name variable
String SqlType;
Data type variable
All three variables are created to create a table statement service.
DataType [] Typearray = {
New DataType (Java.sql.Types.BIT, "BIT"),
New DataType (Java.sql.Types.TINYINT, "TINYINT"),
New DataType (Java.sql.Types.SMALLINT, "SMALLINT"),
New DataType (Java.sql.Types.INTEGER, "INTEGER"),
New DataType (Java.sql.Types.BIGINT, "BIGINT"),
New DataType (Java.sql.Types.FLOAT, "FLOAT"),
New DataType (Java.sql.Types.REAL, "real"),
New DataType (Java.sql.Types.DOUBLE, "DOUBLE"),
New DataType (Java.sql.Types.NUMERIC, "NUMERIC"),
New DataType (Java.sql.Types.DECIMAL, "DECIMAL"),
New DataType (Java.sql.Types.CHAR, "CHAR"),
New DataType (Java.sql.Types.VARCHAR, "VARCHAR"),
New DataType (Java.sql.Types.LONGVARCHAR, "LongVarChar"),
New DataType (Java.sql.Types.DATE, "DATE"),
New DataType (Java.sql.Types.TIME, "time"),
New DataType (Java.sql.Types.TIMESTAMP, "TIMESTAMP"),
New DataType (Java.sql.Types.BINARY, "BINARY"),
New DataType (Java.sql.Types.VARBINARY, "VARBINARY"),
New DataType (Java.sql.Types.LONGVARBINARY, "longvarbinary"),
New DataType (Java.sql.Types.NULL, "NULL"),
New DataType (Java.sql.Types.OTHER, "other"),
};
Create an array to initialize with the code and name of the JDBC SQL data type, others null
Note that all of the names above appear in the types class
DatabaseMetaData Dbmd = Con.getmetadata ();
Create a DatabaseMetaData object to get the properties of the database
ResultSet rs = Dbmd.gettypeinfo ();
Get SQL data types supported by the database
while (Rs.next ()) {
int codenumber = Rs.getint ("Data_type");
String dbmsname = rs.getstring ("type_name");
String createparams = rs.getstring ("Create_params");
The following is the map number of the data types supported by the local database in JDBC, this place name, parameters
System.out.println (codenumber+ "" +dbmsname+ "" +createparams);
for (int i = 0; i < typearray.length; i++) {
if (typearray[i].getcode () = = Codenumber) {
If the element code in Typearray equals the code for the local database type,
You can set the type name in the Typearray to the place names obtained from DatabaseMetaData
Typearray[i].setlocaltypeandparams (
Dbmsname, CreateParams);
System.out.println ("Matching code,sqltype,localtype,params have:");
System.out.println (Typearray[i].getcode () + "" +
Typearray[i].getsqltype () + "" +
Typearray[i].getlocaltype () + "" +
Typearray[i].getparams ());
}
Sets the type name and parameters in the database in the array, printing all matching
}
End for
}//end while
String tablenameprompt = "Input table name" + "and enter";
TableName = GetInput (tablenameprompt);
String createtablestring = "CREATE TABLE" + tablename + "(";
String commaandspace = ",";
Boolean firsttime = true;
while (true) {
System.out.println ("");
String columnnameprompt = "Enter column name +" or do not enter any data and then enter: ";
ColumnName = GetInput (columnnameprompt);
if (firsttime) {
if (columnname.length () = = 0) {
System.out.print ("Need at least one column;");
System.out.println ("Please try Again");
Continue
} else {
Createtablestring + + ColumnName + "";
Continue to form a string to create a table
Firsttime = false;
}
else if (columnname.length () = = 0) {
Break
The process of creating a statement ends when you do not enter a column
} else {
Createtablestring + + Commaandspace
+ ColumnName + "";
}
After the column name is received successfully, the following displays the available type names
System.out.println ("");
System.out.println ("The Available type name is:");
for (int i = 0; i < typearray.length; i++) {
if (! Typearray[i].needstobeset ()) {
If you set the name and parameters in the local database, this JDBC type can be used
System.out.println (Typearray[i].getsqltype ());
Returns the corresponding SQL type name in JDBC
}
}
System.out.println ("");
int index;
while (true) {//loop until input of available types
String typeprompt = "Type of column entered from list" +
"and enter";
SqlType = GetInput (typeprompt);
for (index = 0; index < typearray.length; index++) {
if (Typearray[index].getsqltype ().
Equalsignorecase (SqlType)) {
Compare two strings for equality, case-insensitive
If there is an equal description the type of input is the type that JDBC allows, just jump out
Break
}
}
if (Index < typearray.length) {
If index is less than the number of typearray arrays, a match is indicated
Break
Jump out of while (true) loop
}
System.out.println ("");
System.out.print (SqlType + "does not match the allowable type");

System.out.println ("");
}
String params;
String Localtypename;
params = Typearray[index].getparams ();
Gets the parameter of the type at which the index is the input type in the array
Localtypename = Typearray[index].getlocaltype ();
Get this type name in the database
String paramstring;
String parameterprompt = "Input" + params + ":";
paramstring = "(" + getinput (parameterprompt) + ")";
Createtablestring + + localtypename + paramstring;
Note that creating a table string is a local type name, not a type name in JDBC
I used the name in JDBC when I entered it earlier.
}
Create statement input part end
Createtablestring + = ")";
System.out.println ("");
System.out.print ("the statement you entered to create the table is:");

System.out.println (createtablestring);
System.out.println ("");
Stmt.execute (createtablestring);
Execute a statement that creates a table
Rs=dbmd.getcolumns (NULL, "SYSTEM", Tablename.touppercase (), "%");
SYSTEM.OUT.PRINTLN ("Table information: Table name, column name, class model, type name");
while (Rs.next ()) {
System.out.print (rs.getstring ("table_name") + "");
System.out.print (rs.getstring ("column_name") + "");
System.out.print (Rs.getint ("data_type") + "");
System.out.println (rs.getstring ("type_name"));
}
Rs.close ();
Stmt.close ();
Con.close ();
catch (SQLException ex) {
System.err.println ("SQLException:" + ex.getmessage ());
}
}
public static string GetInput (String prompt) throws SQLException {
System.out.print (prompt);
System.out.flush ();
Clear all characters
try {
Java.io.BufferedReader bin;
To create an object that reads text from a character input stream
bin = new Java.io.BufferedReader (
Character streams as arguments
New Java.io.InputStreamReader (system.in));
Byte stream is a parameter and is converted into character streams
String result = Bin.readline ();
return result;
return string
catch (Exception e) {
System.out.println (e);
Return "";
}
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.