daily pastebin goal
89%
SHARE
TWEET

Untitled

a guest Jan 19th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT TABLE_NAME, ORDINAL_POSITION,
  2. COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE
  3. FROM INFORMATION_SCHEMA.COLUMNS
  4. WHERE TABLE_SCHEMA = {YOUR_DB}
  5. ORDER BY TABLE_NAME, ORDINAL_POSITION;
  6.    
  7. public class getColumnDataTypes  
  8. {  
  9.      public static void main (String[] args) throws Exception
  10.      {
  11.           String driver = "com.mysql.jdbc.Driver";
  12.           String connString = "jdbc:mysql://localhost:3300/DATABASE,USERNAME,PASSWORD";
  13.  
  14.           Class.forName(driver);
  15.           Connection connection = DriverManager.getConnection(connString);
  16.  
  17.           Statement statement = connection.createStatement();
  18.  
  19.           //Query
  20.           ResultSet resultSet = statement.executeQuery("SELCET * FROM table");
  21.  
  22.           ResultSetMetaData rsmd = resultSet.getMetaDate();
  23.  
  24.           //Get number of columns returned
  25.           int numOfCols = rsmd.getColumnCount();
  26.  
  27.           //Print out type for each column
  28.           for(int i=1; i<=numOfCols; ++i)
  29.           {
  30.                System.out.println("Column [" + i + "] data type: " + rsmd.getColumnTypeName(i));
  31.           }
  32.  
  33.           //Close DB connection
  34.           statement.close();          
  35.           connection.close();    
  36.      }
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top