You are on page 1of 5

RMI Menu Driven Console application

Class file
import import import import import import import import import java.io.BufferedReader; java.io.IOException; java.io.InputStreamReader; java.util.Scanner; java.io.*; java.sql.*; java.util.*; org.apache.poi.hssf.usermodel.*; org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class GetHelp { public static void main(String [] s) throws IOException { String userName = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please enter "); userName = br.readLine(); System.out.println(userName); GetHelp gh =new GetHelp(); if(userName.equals("help")){ gh.showMenu(); } } public void showMenu() { Scanner in = new Scanner(System.in); int userChoice; boolean quit = false; do { System.out.println("1. Save "); System.out.println("2. Update"); System.out.println("3. Delete"); System.out.println("4. Display"); System.out.print("Your choice, 0 to quit: "); userChoice = in.nextInt(); switch (userChoice) { case 1: save(); break; case 2: update(); break;

case 3: Delete(); break; case 4: Display(); break; case 0: quit = true; break; default: System.out.println("Wrong choice."); break; } System.out.println(); } while (!quit); System.out.println("Bye!"); } // method to insert record in to database public void save() { String fileName="C:\\Text.xls"; Vector dataHolder=read(fileName); saveToDatabase(dataHolder);

} public static Vector read(String fileName) { Vector cellVectorHolder = new Vector(); try{ FileInputStream myInput = new FileInputStream(fileName); POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); HSSFSheet mySheet = myWorkBook.getSheetAt(0); Iterator rowIter = mySheet.rowIterator(); while(rowIter.hasNext()){ HSSFRow myRow = (HSSFRow) rowIter.next(); Iterator cellIter = myRow.cellIterator(); Vector cellStoreVector=new Vector(); while(cellIter.hasNext()){ HSSFCell myCell = (HSSFCell) cellIter.next(); cellStoreVector.addElement(myCell); } cellVectorHolder.addElement(cellStoreVector); } }catch (Exception e){e.printStackTrace(); } return cellVectorHolder; } private static void saveToDatabase(Vector dataHolder) { String username=""; String password=""; for (int i=0;i<dataHolder.size(); i++){

Vector cellStoreVector=(Vector)dataHolder.elementAt(i); for (int j=0; j < cellStoreVector.size();j++){ HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j); String st = myCell.toString(); username=st.substring(0,5); password=st.substring(5); } try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydemo","root", ""); Statement stat=con.createStatement(); int k=stat.executeUpdate("insert into login(username,password) value('"+username+"','"+password+"')"); System.out.println("Data is inserted user name="+username +" password="+password +""); stat.close(); con.close(); } catch(Exception e){ e.printStackTrace(); } } System.out.println("Record Save successfully"); } // method to update database record public void update() { // add here code for update System.out.println("Record Update successfully"); } // method to delete record from database public void Delete() { //add here code for delete System.out.println("Record Delete successfully"); } //method to display record fron the database public void Display() { try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydemo","root", ""); Statement stat=con.createStatement(); String query = "select * from login"; ResultSet rs=rs = stat.executeQuery(query);

while (rs.next()) { System.out.println("----------------"); System.out.print("| "+rs.getString(1)+" | "); System.out.println(rs.getString(2)+" |"); System.out.println("----------------"); } rs.last(); int rowCount = rs.getRow(); System.out.println("-Number of rows in database-"+rowCount+"\n"); stat.close(); con.close(); } catch(Exception e){ e.printStackTrace(); } System.out.println("Record Display successfully"); } }

Batch file

ECHO OFF ECHO Starting Console Execution Now.... d: cd D:\Gajanan\FIXWorkspace\Try\src\main\java set classpath=D:\jar\commons-codec-1.5.jar;D:\jar\mysql.jar;D:\jar\poi-3.8beta5.jar; ECHO Compiling.... javac GetHelp.java ECHO Running.... java GetHelp ECHO Stop Execution ... ECHO done... pause

You might also like