数据库查询的结果写入规定格式的文本文件?
有张数据库表
名称 大类 数量 单价 总价
数码相机 数码类 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) {
}
%>
写入文本文件后保存在c盘里的效果是:
数码相机 数码类 2
数码相机 数码类 1
数码复印机 数码类 1
U盘 配件类 2
但有什么办法把他写下面格式的?
"数码相机 ", " 数码类 ",2, " "
"数码相机 ", " 数码类 ",1, " "