我正在尝试从数据库中检索数据并以jSON
格式显示出来。但它显示的是空白页。
以下是我在JsonServlet.java
文件中的java代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject;
/**
* Servlet implementation class JsonServlet
*/
@WebServlet("/JsonServlet")
public class JsonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JsonServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
response.setContentType("text/html;charset=UTF-8");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/register","root","toor");
PreparedStatement pstmt = con.prepareStatement(
"select * from headwy " );
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
out.println("hiii");
JSONObject json = new JSONObject();
json.put("phone", rs.getString(1));
json.put("uname", rs.getString(2));
json.put("pass", rs.getString(3));
out.print(json);
}
}
catch (Exception e2) {System.out.println(e2);
}
}
}
我使用MySQl
作为数据库。以下是带有select query的数据库表的数据结果:
select * from head;
+----+-------+--------+-------------+
| id | name | salary | designation |
+----+-------+--------+-------------+
| 1 | srinu | 50000 | test |
| 2 | srinu | 500000 | devel |
| 3 | pawan | 100000 | net |
| 4 | ravi | 2000 | .net |
| 5 | ramya | 22222 | tester |
| 6 | srinu | 0 | sql |
| 7 | srinu | 200000 | test |
| 8 | srinu | 1000 | java |
| 9 | srinu | 11111 | html |
| 10 | srinu | 11111 | html |
| 11 | vv | 29999 | hg |
| 12 | s | 147 | aa |
| 13 | srinu | 5000 | test |
+----+-------+--------+-------------+
我需要帮助才能以正确的JSON
格式显示数据。我们随时欢迎任何建议。
答案 0 :(得分:1)
使用像gson https://github.com/google/gson这样的库来简化json转换。
答案 1 :(得分:0)
您可以这样做:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<HeadObject> heads = new ArrayList<HeadObject>();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
response.setContentType("text/html;charset=UTF-8");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/register","root","toor");
PreparedStatement pstmt = con.prepareStatement("select * from headwy;");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
HeadObject head = new HeadObject();
head.setId(rs.getInt(1));
head.setName(rs.getString(2));
head.setSalary(rs.getString(3));
head.setDesignation(rs.getString(4));
heads.add(head);
}
}
catch (Exception e2) {System.out.println(e2);}
String json = new Gson().toJson(heads);
out.print(json);
}
HeadObject类
public class HeadObject {
int id;
String name;
String salary;
String designation;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}