Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.uic.ids401.util;
- import java.io.*;
- import java.sql.*;
- import java.util.Scanner;
- public class FileIO {
- Scanner keyboard = new Scanner(System.in);
- DBase dbase = new DBase();
- private ResultSet rs;
- private PrintWriter outputStream = null;
- int maxColumn=1;
- public void login()
- {
- Scanner keyboard=new Scanner(System.in);
- System.out.println("What is your username?");
- String userName= keyboard.nextLine();
- System.out.println("What is your password?");
- String password= keyboard.nextLine();
- System.out.println("What is the dbmsServer name");
- String dbmsServer= keyboard.nextLine();
- System.out.println("What is your dbmsType");
- String dbmsType= keyboard.nextLine();
- System.out.println("What is your dbschema");
- String dbSchema= keyboard.nextLine();
- String driver="com.mysql.jdbc.Driver";
- String url = "jdbc:"+dbmsType+"://"+dbmsServer+":3306"+"/"+dbSchema;
- dbase= new DBase(driver,url,userName,password);
- }
- public void query()
- {
- System.out.println("What is your query ");
- rs = dbase.executeSQLdmlQuery(keyboard.nextLine());
- }
- public void result()
- {
- System.out.println("What is your new file name?");
- String name= keyboard.nextLine();
- System.out.println("Your file name is:"+name);
- try{
- outputStream= new PrintWriter(name);
- int numofCol= rs.getMetaData().getColumnCount();
- System.out.println("Your number of columns is:"+numofCol);
- rs.last();
- int rows = rs.getRow();
- System.out.println("Your number of rows is:"+ rows);
- this.maxColumn=numofCol;
- }
- catch(FileNotFoundException e){
- System.out.println("Cannot open file");
- System.exit(0);
- }
- catch (SQLException e1) {
- e1.printStackTrace();
- }
- int curCol = 1; //counter for what column we working with, starts at 1.
- //create headers for columns
- while (curCol <= maxColumn){ //ie...for all columns
- try {
- outputStream.print(rs.getMetaData().getColumnLabel(curCol)+"\t"); //get into metadata and find the column labels...print them followed by a "tab" char.
- rs.beforeFirst(); //return cursor to the non-existant index 0 (it was set to last index by getMeta()
- } catch (SQLException e) {
- e.printStackTrace();
- }
- curCol++; //increments column to set...1..2..3..until maxCol
- }
- outputStream.flush(); //flush the buffer
- outputStream.println(); //create a blank line (so data starts on next line, not line with headers)
- curCol = 1; //reset column counter to 1 because we gonna use it again in next bit
- //end header creation
- //create body for columns
- try {
- rs.next(); //see line 88...we are now moving it to real index 1 here.
- do{
- while (curCol <= maxColumn){ //for all columns...
- outputStream.print(rs.getString(curCol)+"\t"); //write the string at that column + tab
- curCol++; //increment column counter to move to next column, rinse, repeat until colMax.
- }
- outputStream.println(); //go to next line before repeating for next rows
- outputStream.flush(); //flush buffer
- curCol = 1; //reset column counter back to 1
- }while (rs.next());
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement