import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; public class uudisedSetup extends HttpServlet{ static final String HEADER="Setup

Uudised setup

"; static final String FOOTER=""; static final String TYPES_TABLE="create table types (typeid INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, PRIMARY KEY (typeid), UNIQUE (name))"; static final String MESSAGES_TABLE="create table messages (id INT NOT NULL AUTO_INCREMENT, msgauthor VARCHAR(100) NOT NULL, type INT NOT NULL, header VARCHAR(250) NOT NULL, contents TEXT NOT NULL, time TIMESTAMP, PRIMARY KEY (id), FOREIGN KEY typekey(type) REFERENCES types(typeid))"; static final String COMMENTS_TABLE="create table comments (comid INT NOT NULL AUTO_INCREMENT, comauthor VARCHAR(100) NOT NULL , msgid INT NOT NULL, comcontents TEXT NOT NULL, comtime TIMESTAMP, PRIMARY KEY (comid), FOREIGN KEY msgkey(msgid) REFERENCES messages(id))"; static final String[] types_array={"kultuur", "kunst", "teater", "muusika", "haridus", "kirjandus", "poliitika", "tehnika", "veider", "varia"}; static final String HTML_FORM="

"+ "
"+ "
"+ "
"+ "
"+ "
"+ ""+ ""+ ""+ ""+ ""+ "
"; Connection connection; Statement statement; int jobsDone=0; int jobsOrdered=0; void openDatabase() throws Exception{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection=DriverManager.getConnection("jdbc:odbc:uudised", "", ""); statement=connection.createStatement(); } void closeDatabase() throws Exception{ if(connection!=null){ connection.close(); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ mainFunction(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ mainFunction(request, response); } private String executeQuery(String tableName, String query){ jobsOrdered++; try{ statement.executeUpdate(query); jobsDone++; }catch(Exception e){ return "

Error! can't create \""+tableName+"\"!

"+e.getMessage()+"
"; } return "

Table \""+tableName+"\" created!


"; } private String createTypesList(){ jobsOrdered++; try{ PreparedStatement ps=connection.prepareStatement("insert into types (name) values (?)"); for(int i=0; iTypesList created
"; }catch(Exception e){ return "

Error @ TypesList

"+e.getMessage()+"
"; } } private String processRequest(HttpServletRequest request){ StringBuffer result=new StringBuffer(); jobsDone=jobsOrdered=0; if (request.getParameter("types")!=null){ result.append(executeQuery("types", TYPES_TABLE)); } if (request.getParameter("messages")!=null){ result.append(executeQuery("messages", MESSAGES_TABLE)); } if (request.getParameter("comments")!=null){ result.append(executeQuery("comments", COMMENTS_TABLE)); } if (request.getParameter("typeslist")!=null){ result.append(createTypesList()); } if (jobsOrdered>0){ result.append("Back
"); result.append("Jobs ordered: "+jobsOrdered+" and jobs done: "+jobsDone+". "+((float)jobsDone/(float)jobsOrdered*100)+"% of success!
"); }else{ result.append(HTML_FORM); } return result.toString(); } public void mainFunction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ response.setContentType("text/html"); PrintWriter output=response.getWriter(); output.print(HEADER); try{ openDatabase(); output.print(processRequest(request)); closeDatabase(); }catch(Exception e){ output.print("

Viga

"+e.getMessage()); } output.print(FOOTER); } }