Está en la página 1de 16

public class GetActionPage {

public static String getActionFormForInserting(java.util.ArrayList fields) {

String req=""; req=req+"<% \njava.util.ArrayList al= new java.util.ArrayList();\n"; java.util.Iterator it= fields.iterator();

while(it.hasNext()) { String v=it.next().toString(); req=req+"\n String "+v+"="+"request.getParameter(\""+v+"\");\nal.add("+v+");\n";

String req1="\nif(DAO.Logics.insert(al))\n{ response.sendRedirect(\"sucesspage.jsp\");\n}" + "\n else {\n response.sendRedirect(\"error.jsp\");\n} \n %>";

return req+req1;

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package logics; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * * @author SattvaQ */ public class FileGeneration {

public static void createFiles(String filename,String content) { try {

//String content = "This is the content to write into file";

File file = new File(filename); file.getParentFile().mkdirs(); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); }

FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close();

System.out.println("Done");

} catch (IOException e) { e.printStackTrace(); } }

} /* * To change this template, choose Tools | Templates * and open the template in the editor.

*/ package logics;

/** * * @author SattvaQ */ public class GetDBBean { public static String getMySQLDBBean() { String result="package DB;\n" + "import java.sql.Connection;\n" + "import java.sql.DriverManager;\n" + "/**\n" + "*\n" + " * @author SattvaQ */\n" +" public class DB {\n"

+ "public static java.sql.Connection DBConnection()throws Exception\n" + "{" + "Class.forName(\"com.mysql.jdbc.Driver\");\n" + "Connection con=DriverManager.getConnection(\"jdbc:mysql://localhost:3306/sattvaq\",\"root\",\"\"); \n " +" return con;\n"

+ " }\n" + "}\n "; return result;

public static String getOracleDBBean() { String result="package DB;" + "import java.sql.Connection;" + "import java.sql.DriverManager;" + "/**" + "*" + " * @author SattvaQ */" +" public class DB {"

+ "public static java.sql.Connection DBConnection()throws Exception" + "{" + "Class.forName(\"oracle.jdbc.driver.OracleDriver\");" + "Connection con=DriverManager.getConnection(\"jdbc:oracle:thin:@localhost:1521:xe\",\"system\",\"system\"); " +" + " }" + "} "; return result; return con;"

} } /* * To change this template, choose Tools | Templates

* and open the template in the editor. */ package logics;

/** * * @author SattvaQ */ public class GetInputForms { public static String getInputFormHeadings(String action) { String result="<html> <head> " +" <title>"+action +"</title> </head>"

+ " <body>" + " <h1>"+action+"</h1>" + " <table> <form action='"+action.replace(" ","_")+"_action.jsp' method='post' >"; return result;

} public static String getInputFormFooter(String action) { String result="<tr><td>&nbsp;</td><td><input type='submit' value='Submit'/></td></tr></table></form></html></body> "; return result;

}/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package logics;

/** * * @author SattvaQ */ public class GetSQLQueries { public static String getCreateQuery(String tablename,java.util.ArrayList fileds)//,java.util.ArrayList types, java.util.ArrayList sizes, String primarykey) { String query="CREATE TABLE "+tablename+"(\n"; java.util.Iterator itfn=fileds.iterator(); // java.util.Iterator itft=types.iterator(); String fn=""; String ft=""; int s= fileds.size(); int i=0; while(itfn.hasNext())// && itft.hasNext()) {

fn=itfn.next().toString(); if(i==(s-1)) {

query= } else {

query+fn+" varchar(500));\n";

query= } i++;

query+fn+" varchar(500),\n";

/* + "P_Id int NOT NULL," + "LastName varchar(255) NOT NULL," + "FirstName varchar(255)," + "Address varchar(255)," + "City varchar(255)," + "CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName))"; */ return query;

public static String datainsert(String tablename,java.util.ArrayList fields) { java.util.Iterator ital=fields.iterator(); String result="public static boolean insert(java.util.ArrayList data) throws Exception {\n" + "java.sql.Connection con = DBConnection.DB.DBConnection();\n" + "String query = \"\";\n" + "query = \"INSERT INTO `"+tablename+"` " + "VALUES ('\";";

result=result+ "\n int s=data.size(); int i1=0;" + "java.util.Iterator itfields=data.iterator();" + "while(itfields.hasNext())" + "{" + "String v= itfields.next().toString();" + "if(i1==(s-1))" + "{query=query+v+\"')\";" + " }" + " else" + " {query=query+v+\"','\";}i1++; }";

result=result+"\njava.sql.Statement st = con.createStatement();\n";

result=result+"int i = st.executeUpdate(query);\n" + "if (i < 0) \n{\nreturn false;\n}\n else \n{ \nreturn true;\n}\n}"; return result; }

public static String dataRetrieval(String tablename,java.util.ArrayList fields) { java.util.Iterator ital=fields.iterator(); String result="" + "<% java.sql.Connection con = DBConnection.DB.DBConnection();\n" + "String query = \"\";\n" + "query = \"SELECT * FROM `"+tablename+"` \";"; // + "VALUES ('\";"; result=result+"\njava.sql.Statement st = con.createStatement();\n"; result=result+"java.sql.ResultSet rs = st.executeQuery(query);\n";

result=result + "while(rs.next())" +"{ %><tr>";

java.util.Iterator it=fields.iterator();

while(it.hasNext()) { String f= it.next().toString();

result=result+"<td><%=rs.getString(\""+f+"\")%></td>";

} result=result+"</tr><% } %>"+"</table>";

return result; }

} package logics;

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List;

import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;

public class Zipping {

List<String> filesListInDir = new ArrayList<String>();

public static void main(String[] args) { // File file = new File("/Users/pankaj/sitemap.xml"); // String zipFileName = "/Users/pankaj/sitemap.zip";

File dir = new File("C:\\User name"); String zipDirName = "C:\\RapidCode\\1.zip";

//zipSingleFile(file, zipFileName);

Zipping zipFiles = new Zipping(); zipFiles.zipDirectory(dir, zipDirName); }

/** * This method zips the directory * @param dir * @param zipDirName */

public void zipDirectory(File dir, String zipDirName) { try { populateFilesList(dir); //now zip files one by one //create ZipOutputStream to write to the zip file FileOutputStream fos = new FileOutputStream(zipDirName); ZipOutputStream zos = new ZipOutputStream(fos); for (String filePath : filesListInDir) { System.out.println("Zipping " + filePath); //for ZipEntry we need to keep only relative file path, so we used substring on absolute path ZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length() + 1, filePath.length())); zos.putNextEntry(ze); //read the file and write to ZipOutputStream FileInputStream fis = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); fis.close(); } zos.close(); fos.close(); } catch (IOException e) {

e.printStackTrace(); } }

/** * This method populates all the files in a directory to a List * @param dir * @throws IOException */ public void populateFilesList(File dir) throws IOException { File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) { filesListInDir.add(file.getAbsolutePath()); } else { populateFilesList(file); } } }

/** * This method compresses the single file to zip format * @param file * @param zipFileName */

public static void zipSingleFile(File file, String zipFileName) { try { //create ZipOutputStream to write to the zip file FileOutputStream fos = new FileOutputStream(zipFileName); ZipOutputStream zos = new ZipOutputStream(fos); //add a new Zip Entry to the ZipOutputStream ZipEntry ze = new ZipEntry(file.getName()); zos.putNextEntry(ze); //read the file and write to ZipOutputStream FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); }

//Close the zip entry to write to zip file zos.closeEntry(); //Close resources zos.close(); fis.close(); fos.close(); System.out.println(file.getCanonicalPath() + " is zipped to " + zipFileName);

} catch (IOException e) {

e.printStackTrace(); }

} }

También podría gustarte