package test; import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import util.PageDBConnection;
public class DQtest
{
public boolean sqlDriverQuerySaveFileSqlBean() throws Exception
{
boolean sqlDriverQuerySaveFileSqlError = false ;
ResultSet rs = null;
try
{
FileDialog dlg;
Display display = new Display();
System.out.println(display);
Shell shell = new Shell(display);
dlg = new FileDialog(shell, SWT.SAVE);
dlg.setFilterNames(new String[]{
"Text Files(*.txt)",
"Excel Files (*.xls)",
"All Files (*.*)"
});
dlg.setFilterExtensions(new String[] {
"*.txt", "*.xls", "*.*"
});
String fileName = null;
String fn = "";
if (fn.equals(""))
{
[color=#FF0000]fileName = dlg.open();//打开保存窗口[/color]
if(fileName != null)
fn = fileName;
}
if (fn != "")
{
final String SQL = "select * from abc where xm='zhangsan' " ;
System.out.println(SQL);
rs = PageDBConnection.getResultSet(SQL);
byte[] b = null;
FileOutputStream fos = new FileOutputStream(fn);
OutputStreamWriter out = new OutputStreamWriter(fos); out.write(dlg.getText());
ResultSetMetaData meta = rs.getMetaData();
for(int i=1; i<meta.getColumnCount();i++)
{
b = (meta.getColumnName(i) + "\t").getBytes();
fos.write(b);
}
while(rs.next())
{
fos.write("\r\n".getBytes());
for(int i=1;i<meta.getColumnCount();i++)
{
b = (rs.getString(i) + "\t").getBytes();
fos.write(b);
}
sqlDriverQuerySaveFileSqlError = true;
}
fos.close();
out.close();
shell.setText(fn);
MessageBox successBox = new MessageBox(shell);
successBox.setText("信息");
successBox.setMessage("保存文件成功!");
successBox.open();
}
//display.dispose();和display.sleep();
display.dispose();
shell.dispose();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
if (rs != null)
{
try
{
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return sqlDriverQuerySaveFileSqlError;
}
}
这段代码是由点了保存按钮调用的
DriverQuerySaveFileSqlBean dqsfsb = new DriverQuerySaveFileSqlBean();
dqsfsb.sqlDriverQuerySaveFileSqlBeanPage();
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<script language="javascript" type="text/javascript">
function DriverQueryJavaScript(driverQueryButton)
{
var insertButton = 'driverQueryInsertButton' ;
if(driverQueryButton==insertButton)
{
document.forms[0].action='DriverQuery.do?driverQueryType=driverQueryInsertButton' ;
document.forms[0].submit();
}
}
</script>
<head>
<html:base />
<title>驾驶员查询</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>查询条件选择</h2>
<html:errors property="errorDriverQuery"/>
<html:form action="/DriverQuery.do" method="post">
<html:checkbox property="driverQueryCheckBox1" value="driverQuery01">姓名</html:checkbox>
<html:text property="driverQueryNameInput"/>
<br>
<html:button property="DriverQueryInsertButton" onclick="DriverQueryJavaScript('driverQueryInsertButton');">保存</html:button>
</html:form>
</body>
</html:html>