我正在尝试通过Java格式化sql输出,而我以前从未在Java中格式化过。我看了一些教程,但似乎找不到任何能帮助我解决问题的东西。我要删掉大部分代码,所以代码不是很多,但这就是我所拥有的。
File file = new File("WO_SECURITY.doc");
FileWriter fw = new FileWriter(file, false);
BufferedWriter bw = new BufferedWriter(fw);
try {
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
con = DriverManager.getConnection(url,username1,password);
stmt = con.createStatement();
rs = stmt.executeQuery("select usname as Name, \r\n" +
"ususer as Username, \r\n" +
"uswosecl as WO_SecurityLevel, \r\n" +
" aoOpid as Operation_ID,aoseclevel as Operation_Security,\r\n" +
"aomenu as Menu, aomenuitem as Tab, aoText as Description \r\n" +
"from cudtatet.xxpuser \r\n" +
"join fmsusrfua.xxpAuOps on uswosecl >= aoseclevel \r\n" +
"and aoseclevel >=0 \r\n" +
"WHERE ususer NOT IN ('*ALL','daffron') \r\n" +
"and aoAuOpID>=70000 and aoAuOpID < 80000 \r\n" +
"order by usname,ususer ");
while (rs.next()) {
String name = rs.getString(1);
String username = rs.getString(2);
String woSeclevel = rs.getString(3);
String menu = rs.getString(6);
String tab = rs.getString(7);
String Desc = rs.getString(8);
if(!file.exists()) {
file.createNewFile();
}
bw.write("Employee Name: " + name);
bw.newLine();
bw.write("Employee username: " + username);
bw.newLine();
bw.write("WO Security Level: " + woSeclevel);
bw.newLine();
bw.write("Menu: " + menu + " > ");
bw.write(tab + " > ");
bw.write(Desc);
bw.newLine();
bw.newLine();
}
我知道它是多余的,而不是我想要写入文本文件的输出。 我在文本文件上收到的当前输出是。
如果可能,我希望将其格式化为一次以列标题的形式显示员工名称,用户名和安全级别,并在其下方显示菜单,菜单选项卡和菜单说明。 我想要的输出:
如果我能获得有关如何格式化输出以获取所需输出的指导,那将是很棒的!
答案 0 :(得分:0)
我认为解决方案的一部分是:
因此,对于输出,您可以考虑:
private void outputHeader(Writer out, String name, String username, String woSeclevel)
throws IOException
{
String hdr = String.format("Employee Name: %s Username: %s "
+ "WO Security Level %s%n",
name,
username,
woSeclevel);
out.write(hdr);
}
private void ouputLine(Writer out, String menu, String tab, String desc)
throws IOException
{
String line = String.format("Menu: %s > %s > %s%n",
menu,
tab,
desc);
out.write(line);
}
然后您可以这样称呼它:
String lastUsername = null;
while (rs.next) {
String name = rs.getString(1);
String username = rs.getString(2);
String woSeclevel = rs.getString(3);
String menu = rs.getString(6);
String tab = rs.getString(7);
String Desc = rs.getString(8);
// when we change users (or if we haven't seen any user yet)
// then output the header
// if we have processed at least one user, then put space between
// the last output and the new header
if (lastUsername == null || ! lastUsername.equals(username)) {
if (lastUsername != null) {
bw.write("\n\n");
}
outputHeader(bw, name, username, woSeclevel);
}
outputLine(bw, menu, tab, Desc);
// EDIT: this was wrong originally
// we want to update the last seen username to be the username
// of the current record; when the next record is retrieved, then
// the comparison will output a new header if we have moved to
// a different username. NOTE: assumes the records are properly
// ordered
lastUsername = username;
}
通过这种方式,每次用户名更改时都会发出用户的标题(我认为这是更独特的名称本身),而用户的菜单信息将位于标题下方。
请注意,您可能想先按用户名订购。