我这有个servlet程序备份数据库的例子,你的邮箱是多少?
解决方案 »
- struts2框架 数据校验,表单不能显示错误提示。
- 如何在jsp中内嵌的java代码中获得struts2 action中的变量?
- Tomcat5.5.25启动,停止,重起命令
- 怎么用struts2中的标签完成集合迭代
- SERVLET初始化失败停止WEB APP
- 连接池回收问题
- Jsp中分页功能的实现
- 在<select><option>001</option><option>002</option></select>如何使用bean的get方法???
- jsp中文问题求教!
- 怎么在jsp中调用一般的class
- 我用poi包修改excel文件为什么修改不了,而读取却可以?
- 服务器端如何调用Vb中的动态连接库,散所有分(就只有50)
import java.lang.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.sql.*;
public class backupdatabase extends HttpServlet {
static String PosRoot;
HttpServletRequest request;
HttpServletResponse response;
MyDbConnection con;
String PATH="";
String aaa="";
static ServletOutputStream out;
public void init(ServletConfig servletConfig) throws ServletException
{
super.init(servletConfig);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PosRoot = "http://" + request.getHeader("host");
response.setContentType("text/html");
con = new MyDbConnection(out);
out = response.getOutputStream();
aaa=NullToEmpty(request.getParameter("aaa"));
PATH = request.getRealPath("");
writeHeader();
if(!aaa.equals("")){
out.println("<script>");
out.println("window.alert(\"数据备份成功\");");
out.println("</script>");
}
String action = request.getParameter("act");
if(action==null){
writeForm();
}
else if(action.equals("save"))
save(request,response);
writeFoot();
}
//----------------------------------------------------------------------------------------------------------------
private static void alertfunc(){
try{
out.println("<script language=javascript>");
out.println("function backupdb(){");
out.println("var filename=document.form1.filename.value;");
out.println("var filepath=document.form1.filepath.value;");
out.println("if(filename==\"\"){");
out.println("window.alert(\"文件名不能为空\");");
out.println("return false;");
out.println("}");
out.println("else if(filepath==\"\"){");
out.println("window.alert(\"路径不能为空\");");
out.println("return false;");
out.println("}");
out.println("else {");
out.println("return true;}");
out.println("}");
out.println("</script>");
}
catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
}
//--------------------------------------------------------------------------------------------------------------
private void writeHeader(){
try{
out.println("<html>");
out.println("<head><title>Invoice Form</title>");
out.println("<SCRIPT LANGUAGE='JavaScript' SRC='../First.js'></SCRIPT>");
alertfunc();
out.println("</head>");
out.println("<body bgcolor='#CCCCFF' onload=dofocus()>");
out.println("<center>");
out.println("<font Size='2' face='Times New Roman'>");
}catch(Exception e){
System.out.println("error in writeHeader"+e);
}
}
private void writeForm(){
try{
out.println("<br><br><br>");
out.println("<form name=form1 id=form1 method=get onsubmit=\"return backupdb()\">");
out.println("<input type=hidden name=act value=save>");
out.println("<table border cellspacing=0 cellpadding=0 width=500><tr><td>");
out.println("<table bgcolor=#cccccc width=100%>");
out.println("<tr><td> </td></tr>");
out.println("<tr><td align=center><font size=3> 请填写文件名(备份路径为默认,也可以修改)。</font></td></tr>");
out.println("<tr><td align=center>文件名<input type=text name=filename size=50></td></tr>");
out.println("<tr><td align=center>路 径 <input type=text name=filepath value='d:\\java\\backup\\' size=50></td></tr>");
//out.println("<tr><td align=center>路 径 <input type=file name=filepath size=50></td></tr>");
out.println("<tr><td> </td></tr>");
out.println("<tr><td align=center><input type=submit value=' 确定 '> <input type=reset value=' 重置 '></td></tr>");
out.println("</tr><tr><td> </td></tr></table></td></tr></table>");
out.println("</form>");
}
catch(Exception e){
System.out.println("error in writeForm1 "+e);
}
}
//-------------------------------------------------------------------------------------------------------------
private void save(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
String filename=req.getParameter("filename");
String filepath=req.getParameter("filepath");
String file=filepath+filename;
boolean flag=true;
try{
PrintStream excelOutput = null;
excelOutput = new PrintStream(new FileOutputStream(file));
converttable(con,excelOutput,"zcaidan");
converttable(con,excelOutput,"zdiancai");
converttable(con,excelOutput,"zcdh");
converttable(con,excelOutput,"zjs");
converttable(con,excelOutput,"zsrzcmx");
converttable(con,excelOutput,"zclcgd");
//converttable(con,excelOutput,"trademethod");
//converttable(con,excelOutput,"transmeans");
excelOutput.close();
con.CloseMyConnection();
}
catch (Exception ex) {
}
res.sendRedirect(PosRoot + "/****/servlet/backupdatabase?aaa=111");
}
//-------------------------------------------------------------------------------------------------------------
private void converttable(MyDbConnection cone,PrintStream excelOutput,String tablename) throws IOException, SQLException, ServletException, Exception{
try{
String sql = "SELECT * FROM " + tablename;
Statement stmt=null;
ResultSet rs=null;
stmt=cone.MyQueryStatement(sql);
rs=stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
int numcols = rsmd.getColumnCount();
excelOutput.println("&&&" + tablename.trim());
String lineName = "";
for (int i=1;i<=numcols;i++ ) {
String columnName = rsmd.getColumnLabel(i);
lineName += columnName;
lineName += "\t";
}
excelOutput.println(lineName.trim());
while (rs.next()) {
String lineValue = "";
String lineValue1 = "";
for (int i=1;i<=numcols;i++ ) {
String columnName = rsmd.getColumnLabel(i);
String columnValue =NullToEmpty(rs.getString(columnName));
if(columnValue.equals("") || columnValue==null)
columnValue="null";
lineValue += columnValue;
lineValue += "\t";
lineValue1=new String(lineValue.getBytes("ISO-8859-1"),"GBK");
}
excelOutput.println(lineValue1.trim());
}
}catch(Exception e){
}
}
//------------------------------------------------------------------------------------------------------------
private String NullToEmpty(String dd){
if (dd==null||dd.equals("null"))
return "";
else
return dd.trim();
}
//------------------------------------------------------------------------------------------------------------
private void writeFoot(){
try{
out.println("</font>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
}catch(Exception e){
System.out.println("error in write output"+e);
}
}
}