/Users/lyon/j4p/src/net/server/servlets/SqlSynthesizer.java

1    package net.server.servlets; 
2     
3    import javax.servlet.http.HttpServletRequest; 
4    import java.io.FileWriter; 
5    import java.io.IOException; 
6    import java.util.Vector; 
7     
8    /** 
9     * The SqlSynthesizer class encapsulates the functionality 
10    * required to construct sql statements which can then 
11    * be submitted to a database for processing. 
12    * 
13    * @author Robert Lysik 
14    * @version 1.00 
15    */ 
16   class SqlSynthesizer { 
17       private String[] sqlStatements; 
18    
19       /** 
20        * This is the constructor for the SqlSynthesizer class. 
21        * An object of type HttpServletRequest is passed in as 
22        * a parameter. This object is parsed for information which 
23        * is used to construct the SQL statements. 
24        */ 
25       SqlSynthesizer(HttpServletRequest request) { 
26           Vector sqlStatementVector = new Vector(); 
27    
28           String course = new String(); 
29           String section = new String(); 
30           String term = new String(); 
31           String year = new String(); 
32           String sqlStatement = new String(); 
33    
34           int rowCount = Integer.parseInt(request.getParameter("rows")); 
35           int colCount = Integer.parseInt(request.getParameter("cols")); 
36    
37           course = request.getParameter("course"); 
38           section = request.getParameter("section"); 
39           term = request.getParameter("term"); 
40           year = request.getParameter("year"); 
41    
42           String[] students = new String[rowCount + 1]; 
43           String[][] value = new String[rowCount + 1][colCount]; 
44    
45           for (int index = 0; index < rowCount; index++) 
46               students[index] = request.getParameter("recordnum" + index); 
47    
48           for (int rowIndex = 1; rowIndex <= rowCount; rowIndex++) 
49               for (int colIndex = 1; colIndex < colCount; colIndex++) 
50                   value[rowIndex][colIndex] = 
51                           request.getParameter("r" + rowIndex + "c" + colIndex); 
52    
53           for (int rowIndex = 1; rowIndex <= rowCount; rowIndex++) 
54               for (int colIndex = 1; colIndex < colCount; colIndex++) { 
55                   sqlStatement = "insert into all_forms values (" + 
56                           students[rowIndex - 1] + ", '" + 
57                           course + "', '" + 
58                           section + "', '" + 
59                           term + "', '" + 
60                           year + "', " + 
61                           "1, '" + 
62                           "" + colIndex + "', " + 
63                           value[rowIndex][colIndex] + ");\r\n"; 
64    
65    
66                   sqlStatementVector.add(sqlStatement); 
67               } 
68    
69           sqlStatements = new String[sqlStatementVector.size()]; 
70    
71           for (int index = 0; index < sqlStatementVector.size(); index++) 
72               sqlStatements[index] = (String) sqlStatementVector.get(index); 
73       } 
74    
75       /** 
76        * This method constructs an insert statement based upon the 
77        * table name and values which are passed in as parameters. 
78        * 
79        */ 
80       public String GetInsertStatement(String table, String[] values) { 
81           String statement = "insert into " + table + " values ("; 
82    
83           for (int index = 0; index < values.length - 1; index++) 
84               statement += values[index] + ", "; 
85    
86           statement += values[values.length - 1] + ");"; 
87    
88           return statement; 
89       } 
90    
91       /** 
92        * This method saves the SQL statements which have been 
93        * generated to a text file whose name is passed in as a parameter. 
94        * 
95        */ 
96       public void save(String fileName) { 
97           FileWriter writer = null; 
98    
99           try { 
100              writer = new FileWriter(fileName); 
101          } catch (IOException ioe) { 
102              ioe.printStackTrace(); 
103          } 
104   
105          try { 
106              for (int index = 0; index < sqlStatements.length; index++) 
107                  writer.write(sqlStatements[index]); 
108   
109              writer.close(); 
110          } catch (IOException ioe) { 
111              ioe.printStackTrace(); 
112          } 
113      } 
114   
115      /** 
116       * This method writes the SQL statements which have been generated 
117       * to the output console. 
118       */ 
119      public void print() { 
120          for (int index = 0; index < sqlStatements.length; index++) 
121              System.out.println(sqlStatements[index]); 
122      } 
123  }