gpt4 book ai didi

java - 如何格式化从数据库检索的数据

转载 作者:行者123 更新时间:2023-12-02 11:59:55 26 4
gpt4 key购买 nike

请帮助我格式化从数据库检索的数据。我想从数据库检索数据并将其显示在网页中。这里使用的数据库是postgres。netbeans平台用于开发Web应用程序。

我得到的输出为单行

eg-SSN :1 Subbu TVM SSN :2 Kailas KZH

我想要它在不同的行

eg-
SSN :1 Subbu TVM
SSN :2 Kailas KZH

我的代码:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class view extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>All Employees</title></head>");
out.println("<body>");
out.println("<center><h1>All Employees</h1>");
String ls=System.getProperty("line.separator");
Connection con = null;
Statement stmt = null;
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Project","postgres","1234");
stmt = con.createStatement();


ResultSet rs = stmt.executeQuery("SELECT * FROM chef;");
while (rs.next()) {
String ssn = rs.getString("ssn");
String cname = rs.getString("cname");
String allocation = rs.getString("allocation");
out.println("SSN :"+ssn+" "+cname+" "+allocation);

//out.print("ssn : " + ssn +"\n" );
//out.print("name : "+cname+"\n" );
//out.print("allocation : "+allocation+"\n" );

}
rs.close();

} catch (SQLException e) {
out.println("An error occured while retrieving " + "all employees: "
+ e.toString());
} catch (ClassNotFoundException e) {
throw (new ServletException(e.toString()));
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
}
}
out.println("</center>");
out.println("</body>");
out.println("</html>");
out.close();
}
}

最佳答案

Just use <br/>

out.write("<br />");
out.write("SSN :"+ssn+" "+cname+" "+allocation);

关于java - 如何格式化从数据库检索的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47349134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com