Dynamic View Generation using MySQL - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Dynamic View Generation using MySQL

Share This



package com.t4b.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; public class TestMain { public static void main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } try { Connection connection = DriverManager .getConnection("jdbc:mysql://localhost:3307/test"); PreparedStatement pStatement = connection .prepareStatement("select * from userstab"); ResultSet rs = pStatement.executeQuery(); ResultSetMetaData rSetMetaData = rs.getMetaData(); int colCount = rSetMetaData.getColumnCount(); System.out.println("Columns are: "); for (int c = 1; c <= colCount; c++) { System.out.print(" " + rSetMetaData.getColumnName(c)); System.out.print(" " + rSetMetaData.getColumnDisplaySize(c)); System.out.print(" " + rSetMetaData.getColumnTypeName(c)); System.out.println(); } while (rs.next()) { for (int c = 1; c <= colCount; c++) { if (rSetMetaData.getColumnTypeName(c).equalsIgnoreCase( "VARCHAR")) { System.out.print(" " + rs.getString(c)); } else if (rSetMetaData.getColumnTypeName(c) .equalsIgnoreCase("NUMBER")) { System.out.print(" " + rs.getInt(c)); } else { // ...... } } } } catch (SQLException e) { e.printStackTrace(); } } }

Happy Exploring!



No comments:

Post a Comment