数据库查询的结果写入文本文件?
有张数据库表
名称 大类 数量 单价 总价
数码相机 数码类 2 2500 5000
数码相机 数码类 1 2600 2600
数码复印机 数码类 1 9000 9000
U盘 配件类 2 200 400
怎么用jsp查询后写入文本文件后保存在c盘里。
写入的的效果是:
数码相机 数码类 2 2500 5000
数码相机 数码类 1 2600 2600
数码复印机 数码类 1 9000 9000
U盘 配件类 2 200 400
------解决方案-------------------- <%
String dbUrl = "jdbc:oracle:thin:@serv:1521:servname ";
String dbUser = "scott ";
String dbPass = "tiger ";
String driverName = "oracle.jdbc.OracleDriver ";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
StringBuffer strBuf = new StringBuffer();
try {
Class.forName( "oracle.jdbc.driver.OracleDriver ");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
st = conn.createStatement();
rs = st.executeQuery( "select * from emp ");
while (rs.next()) {
strBuf.append(rs.getString( "yourFieldName1 ")+ "\t ");
strBuf.append(rs.getString( "yourFieldName2 ")+ "\t ");
strBuf.append(rs.getString( "yourFieldName3 ")+ "\t ");
strBuf.append( "\r\n ");
}
RandomAccessFile rf = new RandomAccessFile( "c:\\db.txt ", "rw ");
rf.write(strBuf.toString().getBytes());
rf.close();
rs.close();
st.close();
conn.close();
} catch (Exception e) {
}
%>
你参考着改一下就可以了