Database related
Some things that I need to work on, related to Database.
These code is used only to solve my needs, not suitable for production.
These code is used only to solve my needs, not suitable for production.
- Print data of a table
private static void printTableDetails() { String tableName = "Student"; Connection con=null; try { Class.forName("com.vertica.jdbc.Driver"); Properties props = new Properties(); props.put("user", "xxx"); props.put("password", "xxx"); con=DriverManager.getConnection("jdbc:xxx://host:port/",props); System.out.println("### Metadata of table : " + tableName); DatabaseMetaData databaseMetaData = con.getMetaData(); ResultSet columns = databaseMetaData.getColumns(null,null, tableName, null); int count = 1; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from " + tableName); ResultSetMetaData rsmd = rs.getMetaData(); ListcolumnNames = new ArrayList (); while(columns.next()) { String columnName = columns.getString("COLUMN_NAME"); columnNames.add(columnName); String columnSize = columns.getString("COLUMN_SIZE"); String datatype = columns.getString("DATA_TYPE"); String isNullable = columns.getString("IS_NULLABLE"); String isAutoIncrement = columns.getString("IS_AUTOINCREMENT"); String column_type_name = rsmd.getColumnTypeName(count); ++count; System.out.println("Column Name : " + columnName + ", \tData Type : " + column_type_name); } System.out.println("--------"); ResultSet rs1 = stmt.executeQuery("Select * from " + tableName); System.out.println("Data of table : " + tableName); // Print column names for(String colName : columnNames) { System.out.print(colName + " \t\t"); } System.out.println(""); // Print table data while(rs1.next()) { for(String colName : columnNames) { System.out.print(rs1.getString(colName) + " \t\t"); } System.out.println(""); } } catch(Exception e) { e.printStackTrace(); } finally { if(con != null) { try { con.close(); } catch(Exception ex) {} } } }
Comments
Post a Comment