Java/Database SQL JDBC/JDBC Data Type
Содержание
- 1 converting a java.sql.Types integer value into a printable name
- 2 Determining the Type of a Character: determine the properties of a character for the entire Unicode character set.
- 3 Enumeration Type: JDBC
- 4 Get the database-specific type name
- 5 Get the java.sql.Types type to which this database-specific type is mapped
- 6 Getting the Name of a JDBC Type
- 7 Listing Available SQL data Types Used by a Database
- 8 Retrieve type info from the result set
- 9 Save Object JDBC
- 10 uses reflection to get all the field names from java.sql.Types.
converting a java.sql.Types integer value into a printable name
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTypeInfo();
while (resultSet.next()) {
String typeName = resultSet.getString("TYPE_NAME");
short dataType = resultSet.getShort("DATA_TYPE");
getJdbcTypeName(dataType);
}
}
public static void getJdbcTypeName(int jdbcType) {
Map map = new HashMap();
// Get all field in java.sql.Types
Field[] fields = java.sql.Types.class.getFields();
for (int i = 0; i < fields.length; i++) {
try {
String name = fields[i].getName();
Integer value = (Integer) fields[i].get(null);
map.put(value, name);
} catch (IllegalAccessException e) {
}
}
System.out.println(map);
}
}
Determining the Type of a Character: determine the properties of a character for the entire Unicode character set.
public class Main {
public static void main(String[] argv) throws Exception {
char ch = "a";
if (Character.isLetter(ch)) { // true
}
if (Character.isDigit(ch)) { // false
}
if (Character.isLowerCase(ch)) { // true
}
if (Character.isUpperCase(ch)) { // false
}
}
}
Enumeration Type: JDBC
/*
MySQL and Java Developer"s Guide
Mark Matthews, Jim Cole, Joseph D. Gradecki
Publisher Wiley,
Published February 2003,
ISBN 0471269239
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class EnumTesting {
Connection connection;
Statement statement;
public EnumTesting() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager
.getConnection("jdbc:mysql://192.168.1.25/test?user=spider&password=spider");
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
public void doWork() {
try {
statement = connection.createStatement();
ResultSet rs = statement
.executeQuery("SHOW COLUMNS FROM enumtest LIKE "status"");
rs.next();
String enums = rs.getString("Type");
System.out.println(enums);
int position = 0, count = 0;
String[] availableEnums = new String[10];
while ((position = enums.indexOf(""", position)) > 0) {
int secondPosition = enums.indexOf(""", position + 1);
availableEnums[count++] = enums.substring(position + 1,
secondPosition);
position = secondPosition + 1;
System.out.println(availableEnums[count - 1]);
}
rs.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Enum e = new Enum();
e.doWork();
}
}
Get the database-specific type name
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTypeInfo();
while (resultSet.next()) {
// Get the database-specific type name
String typeName = resultSet.getString("TYPE_NAME");
short dataType = resultSet.getShort("DATA_TYPE");
}
}
}
Get the java.sql.Types type to which this database-specific type is mapped
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTypeInfo();
while (resultSet.next()) {
String typeName = resultSet.getString("TYPE_NAME");
short dataType = resultSet.getShort("DATA_TYPE");
}
}
}
Getting the Name of a JDBC Type
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTypeInfo();
while (resultSet.next()) {
String typeName = resultSet.getString("TYPE_NAME");
short dataType = resultSet.getShort("DATA_TYPE");
getJdbcTypeName(dataType);
}
}
public static void getJdbcTypeName(int jdbcType) {
Map map = new HashMap();
// Get all field in java.sql.Types
Field[] fields = java.sql.Types.class.getFields();
for (int i = 0; i < fields.length; i++) {
try {
String name = fields[i].getName();
Integer value = (Integer) fields[i].get(null);
map.put(value, name);
} catch (IllegalAccessException e) {
}
}
System.out.println(map);
}
}
Listing Available SQL data Types Used by a Database
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTypeInfo();
while (resultSet.next()) {
// Get the database-specific type name
String typeName = resultSet.getString("TYPE_NAME");
// Get the java.sql.Types type to which this database-specific type is mapped
short dataType = resultSet.getShort("DATA_TYPE");
}
}
}
Retrieve type info from the result set
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTypeInfo();
while (resultSet.next()) {
String typeName = resultSet.getString("TYPE_NAME");
short dataType = resultSet.getShort("DATA_TYPE");
}
}
}
Save Object JDBC
/*
MySQL and Java Developer"s Guide
Mark Matthews, Jim Cole, Joseph D. Gradecki
Publisher Wiley,
Published February 2003,
ISBN 0471269239
*/
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Thumbnail {
Connection connection;
PreparedStatement statement;
public Thumbnail() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager
.getConnection("jdbc:mysql://192.168.1.25/identification?user=spider&password=spider");
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
public void doWork(String[] args) {
try {
byte[] bytes = new byte[50000];
FileInputStream fs = new FileInputStream(args[2]);
BufferedInputStream bis = new BufferedInputStream(fs);
bis.read(bytes);
ID id = new ID();
id.nail_id = Integer.parseInt(args[0]);
id.acc_id = Integer.parseInt(args[1]);
statement = connection
.prepareStatement("INSERT INTO thumbnail VALUES(?,?,?,?, 0, now())");
statement.setInt(1, id.nail_id);
statement.setInt(2, id.acc_id);
statement.setBytes(3, bytes);
statement.setObject(4, id);
int i = statement.executeUpdate();
System.out.println("Rows updated = " + i);
bis.close();
fs.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thumbnail nail = new Thumbnail();
nail.doWork(args);
}
}
class ID implements Serializable {
public int nail_id;
public int acc_id;
public byte[] bytes;
public ID() {
}
}
uses reflection to get all the field names from java.sql.Types.
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTypeInfo();
while (resultSet.next()) {
// Get the database-specific type name
String typeName = resultSet.getString("TYPE_NAME");
// Get the java.sql.Types type to which this database-specific type is
// mapped
short dataType = resultSet.getShort("DATA_TYPE");
getJdbcTypeName(dataType);
}
}
public static void getJdbcTypeName(int jdbcType) {
Map map = new HashMap();
// Get all field in java.sql.Types
Field[] fields = java.sql.Types.class.getFields();
for (int i = 0; i < fields.length; i++) {
try {
String name = fields[i].getName();
Integer value = (Integer) fields[i].get(null);
map.put(value, name);
} catch (IllegalAccessException e) {
}
}
System.out.println(map);
}
}