gpt4 book ai didi

java - 如何使用 Java 在 Eclipse 中使用 Web 服务(JAX - RS)从数据库中检索数据

转载 作者:可可西里 更新时间:2023-11-01 07:35:46 24 4
gpt4 key购买 nike

我已将一条记录插入数据库,但我不知道如何检索它。我的代码是:账户.java:

package com.fetch;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;

public class Account implements Serializable
{
@Id
private long id;

@Column(name="NAME")
private String name;

public Account()
{

}

public Account(int id, String name)
{
this.id = id;
this.name = name;
}

public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
}

我的账户.java:

package com.fetch;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@WebService()
@Entity
@Table(name = "CUSTOMER")
@Path("/user")
@NamedQuery(name="loginquery", query="select ID,NAME from CUSTOMER")
public class MyAccount
{
private Account ac;
public MyAccount()
{
// TODO Auto-generated constructor stub
ac = new Account();
}

@POST
@Path("/fetch")
@WebMethod(operationName = "insert")
public String insert(@FormParam("name") String name)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo", "root", "root");

String query = "insert into CUSTOMER"+"(NAME) VALUES"+"(?)";
PreparedStatement st = con.prepareStatement(query);
st.setString(1,name);
st.executeUpdate();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return"Record inserted successfully";
}
public Account getAc()
{
return ac;
}
public void setAc(Account ac)
{
this.ac = ac;
}
}

插入.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert</title>
</head>
<body>
<form action="rest/user/fetch" method="POST">
<p>
Name : <input id="name" name="name" />
</p>
<input type="submit" value="Add" />
<input type="submit" value="Retrive" />
</form>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FetchAndInsert</display-name>
<servlet>
<servlet-name>REST Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.fetch</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

当用户点击检索按钮时,如何从数据库中检索数据,并以另一个HTML 表单显示所有记录?请就如何操作给出建议。

在我的应用程序中,当用户单击检索按钮时,它正在执行插入操作。但我想要的是,当用户点击它时,它应该转到另一个页面并显示数据库表中的数据。

谁能告诉我我所做的错误,并给出解决方案的建议,以及当用户单击检索按钮时如何在其他页面显示动态表?

最佳答案

我终于完成了,现在我可以使用 jersey(JAX - RS) web 服务从数据库中插入和检索数据,并能够在 html 页面中显示它。

账户.java:

package com.fetch;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;

public class Account implements Serializable
{
@Id
private long id;

@Column(name = "NAME")
private String name;

public Account(int id, String name)
{
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

我的账户.java:

package com.fetch;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.Entity;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@WebService()
@Entity
@Path("/user")
public class MyAccount
{
@POST
@Path("/fetch")
@WebMethod(operationName = "insert")
public String insert(@FormParam("name") String name)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo", "root", "root");
String query = "insert into CUSTOMER"+"(NAME) VALUES"+"(?)";

PreparedStatement st = con.prepareStatement(query);
st.setString(1,name);
st.executeUpdate();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return"Record inserted successfully";
}

@GET
@Path("/retrive")
@Produces("text/html")
@WebMethod(operationName = "retrive")
public String retrive()
{
ResultSet rs = null;
String details = "";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo", "root", "root");

String query = "select ID,NAME from CUSTOMER";

PreparedStatement st = con.prepareStatement(query);
rs = st.executeQuery();

details = "<html><body>";
details = details + "<table border=1>";
details = details + "<tr><td><Strong>Id </Strong></td>" +
"<td><Strong>Name </Strong></td>" + "</tr>";
while (rs.next())
{
details = details + "<tr><td>" + rs.getInt("ID") + "</td>" +
"<td>" + rs.getString("NAME") + "</td></tr>";
}
details += "</table></body></html>";
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return details;
}
}

插入.html:

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert</title>
</head>
<body>
<form action="rest/user/fetch" method="POST">
<p>
Name : <input id="name" name="name" />
</p>
<input type="submit" value="Add" />
</form>
<form action="rest/user/retrive" method="GET">
<input type="submit" value="Retrive" />
</form>
</body>
</html>

我的 web.xml 是:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>FetchAndInsert</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>REST Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.fetch</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

关于java - 如何使用 Java 在 Eclipse 中使用 Web 服务(JAX - RS)从数据库中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10091499/

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