Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package edu.uic.ids401.util;
  2.  
  3. import java.io.*;
  4. import java.sql.*;
  5. import java.util.Scanner;
  6.  
  7. public class FileIO {
  8. Scanner keyboard = new Scanner(System.in);
  9. DBase dbase = new DBase();
  10. private ResultSet rs;
  11. private PrintWriter outputStream = null;
  12. int maxColumn=1;
  13.  
  14. public void login()
  15.  
  16. {
  17. Scanner keyboard=new Scanner(System.in);
  18. System.out.println("What is your username?");
  19. String userName= keyboard.nextLine();
  20. System.out.println("What is your password?");
  21. String password= keyboard.nextLine();
  22. System.out.println("What is the dbmsServer name");
  23. String dbmsServer= keyboard.nextLine();
  24. System.out.println("What is your dbmsType");
  25. String dbmsType= keyboard.nextLine();
  26. System.out.println("What is your dbschema");
  27. String dbSchema= keyboard.nextLine();
  28.  
  29.  
  30.  
  31. String driver="com.mysql.jdbc.Driver";
  32. String url = "jdbc:"+dbmsType+"://"+dbmsServer+":3306"+"/"+dbSchema;
  33. dbase= new DBase(driver,url,userName,password);
  34. }
  35. public void query()
  36. {
  37. System.out.println("What is your query ");
  38. rs = dbase.executeSQLdmlQuery(keyboard.nextLine());
  39.  
  40.  
  41. }
  42. public void result()
  43. {
  44. System.out.println("What is your new file name?");
  45. String name= keyboard.nextLine();
  46. System.out.println("Your file name is:"+name);
  47.  
  48. try{
  49. outputStream= new PrintWriter(name);
  50. int numofCol= rs.getMetaData().getColumnCount();
  51. System.out.println("Your number of columns is:"+numofCol);
  52. rs.last();
  53. int rows = rs.getRow();
  54. System.out.println("Your number of rows is:"+ rows);
  55. this.maxColumn=numofCol;
  56. }
  57. catch(FileNotFoundException e){
  58. System.out.println("Cannot open file");
  59. System.exit(0);
  60. }
  61. catch (SQLException e1) {
  62. e1.printStackTrace();
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. int curCol = 1; //counter for what column we working with, starts at 1.
  71.  
  72. //create headers for columns
  73. while (curCol <= maxColumn){ //ie...for all columns
  74. try {
  75. outputStream.print(rs.getMetaData().getColumnLabel(curCol)+"\t"); //get into metadata and find the column labels...print them followed by a "tab" char.
  76. rs.beforeFirst(); //return cursor to the non-existant index 0 (it was set to last index by getMeta()
  77.  
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81. curCol++; //increments column to set...1..2..3..until maxCol
  82. }
  83. outputStream.flush(); //flush the buffer
  84. outputStream.println(); //create a blank line (so data starts on next line, not line with headers)
  85. curCol = 1; //reset column counter to 1 because we gonna use it again in next bit
  86. //end header creation
  87.  
  88. //create body for columns
  89.  
  90. try {
  91. rs.next(); //see line 88...we are now moving it to real index 1 here.
  92. do{
  93.  
  94. while (curCol <= maxColumn){ //for all columns...
  95. outputStream.print(rs.getString(curCol)+"\t"); //write the string at that column + tab
  96. curCol++; //increment column counter to move to next column, rinse, repeat until colMax.
  97. }
  98. outputStream.println(); //go to next line before repeating for next rows
  99. outputStream.flush(); //flush buffer
  100.  
  101. curCol = 1; //reset column counter back to 1
  102. }while (rs.next());
  103.  
  104. } catch (SQLException e) {
  105. // TODO Auto-generated catch block
  106. e.printStackTrace();
  107. }
  108.  
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement