package javatemplate1; // MYSQL table mysqlVisitor1A // Using Thread Sleep import java.sql.*; import java.sql.ResultSet; // for result set import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; public class JavaTemplate1 { // example of public static void main(String[] args) throws InterruptedException { // TODO code application logic here System.out.println("main block executing "); Connection c = null; //manage copnnection Statement st = null;// query statement support ResultSet rs = null;// active connection database result //String dbURL = "jdbc:postgresql://localhost:5432/pgsdemo1"; // String user = "postgres"; String pwd = "postgre_manas9"; String dbURL = "jdbc:mysql://localhost/test?"; String user = "Manas9"; String pwd = "Manas9237"; String sqlCreate = "create table mysqlVisitor1A(" + "idV1A int(3) NOT NULL PRIMARY KEY," + "fname varchar(30)," + "lname varchar(30)," + "age int(2)NOT NULL" +") ENGINE=InnoDB DEFAULT CHARSET=utf8;"; String sqldrop= "drop table if exists mysqlVisitor1A"; String sqlQuery ="Select * from mysqlVisitor1A"; String strInsert0 ="INSERT INTO mysqlVisitor1A values (101,'John', 'Doe', 40)"; String strInsert1="INSERT INTO mysqlVisitor1A values(102,'Peter', 'Pan', 44)"; String strInsert2 ="INSERT INTO mysqlVisitor1A values(103,'Jean', 'Zenis', 29)"; String strInsert3 ="INSERT INTO mysqlVisitor1A values(104,'Savita', 'Jha', 34)"; String [] strInsert = new String[]{strInsert1, strInsert2, strInsert3, strInsert0}; String strBuff = null; // connecting to db try { // connection>>statement >> Resultset c = DriverManager.getConnection(dbURL,user,pwd); // drop table if exists visitor1A; st = c.createStatement(); st.execute(sqldrop); //create table mysqlVisitor1B( st = c.createStatement(); st.execute(sqlCreate); // for(int i =0; i< strInsert.length; i++) { strBuff = strInsert[i]; st = c.createStatement(); st.execute(strBuff); } //Select * from mysqlVisitor1B st = c.createStatement(); rs = st.executeQuery(sqlQuery); ResultSetMetaData metaData = rs.getMetaData(); int numberOfColumns = metaData.getColumnCount(); //int rownum = metaData. // get name of the columuns for ( int i = 1; i <= numberOfColumns; i++ ) { System.out.printf( "%-4s\t", metaData.getColumnName( i ) ); } System.out.println(); // Process.loaddata(rs, numberOfColumns); // Process.loaddata(rs, numberOfColumns); System.out.println("dropping table at the end"); Thread.sleep(50000); //dropping table at the end st = c.createStatement(); st.execute(sqldrop); Process.loaddata(rs, numberOfColumns); } catch (SQLException ex) { ex.getErrorCode(); String message = ex.getMessage(); System.out.println(message); } finally { System.out.println("going through final block"); try { if (st== null || st.isClosed()) { } else { st.close(); } if (c != null && !c.isClosed()) { c.close(); } } catch (SQLException ex) { ex.getErrorCode(); ex.getMessage(); } } System.out.println("dis-Connected to database successfully"); } } class Process { // public static void loaddata(ResultSet x1 , int n2) throws SQLException { int size = 0; while ( x1.next() ) { size++; for ( int i = 1; i <= n2; i++ ) try { System.out.printf( "%-4s\t", x1.getObject( i )); } catch (SQLException ex) { Logger.getLogger(Process.class.getName()). log(Level.SEVERE, null, ex); } System.out.println(); } //displayMap(list1); System.out.println("processed rows :: "+ size); } }