用于使用 MySQL 数据库的 REST Web 服务

发布于 2024-12-10 04:46:05 字数 1791 浏览 1 评论 0原文

我正在使用 JAX-RS 和 Tomcat 构建 REST WebService 来使用 MySQL 数据库。

我遵循这个模型:

@Path("/login")  
public class Login {

String username;
String password;
// This method is called if POST is requested
@POST
@Produces(MediaType.APPLICATION_XML)
public String loginResponseXML(@FormParam("username") String user, @FormParam("password") String pass) {

    //Connection to MySQL Database
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/sakila", "root","larcom");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("Select first_name, last_name From actor where first_name='" +
                                        user + "' and last_name='" + pass + "'");

        while (rs.next()){
            System.out.println(rs.getString("first_name") + " " + rs.getString("last_name"));
            username = rs.getString("first_name");
            password = rs.getString("last_name");
        }
    }           
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    if (user.equals(username) && pass.equals(password)) {
        return ("<?xml version=\"1.0\"?>" + "<auth>200" + "</auth>"); //Success
        //return "Success!";
    } else {
        return ("<?xml version=\"1.0\"?>" + "<auth>404" + "</auth>"); //Damn
        //return "Damn!";
    }

    }
}

我将此方法称为:

HttpPost httppost = new HttpPost("http://192.168.15.245:8080/org.jersey.andre/rest/login");

现在,我的问题是: 如果我想在数据库中查询另一个表,我必须创建一个新类(例如 Login)并再次建立 JDBC 连接? 一个新类和每个对数据库进行查询的类的新 JDBC 连接?性能问题?

希望你能理解。 提前致谢。

I'm building a REST WebService with JAX-RS and Tomcat to consume a MySQL Database.

I'm following this model:

@Path("/login")  
public class Login {

String username;
String password;
// This method is called if POST is requested
@POST
@Produces(MediaType.APPLICATION_XML)
public String loginResponseXML(@FormParam("username") String user, @FormParam("password") String pass) {

    //Connection to MySQL Database
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/sakila", "root","larcom");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("Select first_name, last_name From actor where first_name='" +
                                        user + "' and last_name='" + pass + "'");

        while (rs.next()){
            System.out.println(rs.getString("first_name") + " " + rs.getString("last_name"));
            username = rs.getString("first_name");
            password = rs.getString("last_name");
        }
    }           
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    if (user.equals(username) && pass.equals(password)) {
        return ("<?xml version=\"1.0\"?>" + "<auth>200" + "</auth>"); //Success
        //return "Success!";
    } else {
        return ("<?xml version=\"1.0\"?>" + "<auth>404" + "</auth>"); //Damn
        //return "Damn!";
    }

    }
}

I call this method with:

HttpPost httppost = new HttpPost("http://192.168.15.245:8080/org.jersey.andre/rest/login");

Now, my question is:
If I want to query the DB for another table I have to create a new class like Login and make the JDBC connection again?
A new class and a new JDBC connection for each class that make a query to the DB? Performance issues?

Hope you can understand.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-12-17 04:46:05

这里有一些提示:请将基于数据库的代码隔离到“数据层”,可以这么说......仅在资源类中执行调度/业务逻辑。

现在,如果您查询不同的表,您将得到不同的查询!您可以使用相同的连接(错误)或创建一个新连接并触发不同的查询。

现在,每个资源是否访问不同的表或具有不同查询的同一表取决于您对该资源的“表示形式”的选择。 RDB 模式具有多个表是有原因的,并且很常见的是,您会有涉及多个表或相互独立的表的不同查询。

性能问题:可以说,对于“新鲜数据”,您总是会访问数据库。如果您想优化,要么开发自己的缓存(非常困难),要么使用 memcached 或 ehcache 等方法来提高性能 - 在决定这样做之前,请确保验证它是否值得。

每秒大约有 1000 次数据库点击吗?您可能需要一些性能提升/处理。每天……也许不是。每 2-3 天...YAGNI(您不会需要它,所以现在不用担心)

因此,对于您在应用程序中设计的每个“资源”(登录不是资源:请参阅相关帖子: 为什么基于表单的身份验证不被视为 RESTful?)选择代表。它可能涉及不同的查询等,让您返回json/xml/xhtml(无论您选择什么)。每个“与数据库相关的调用”都应该隔离到它自己的“数据层”中 - 我建议使用 Spring JDBC 让您的生活更轻松。它将减轻您肩上的 JDBC 管道负担,以便您可以专注于创建 DAO(数据访问对象 - 数据访问类的一种模式。所有 DAO 逻辑上都属于数据层)

希望这会有所帮助

A few tips are in order here: Please isolate the DB based code to a "data layer" so to speak...only perform dispatching/business logic within your resource classes.

Now If you are querying a different table, you WILL have a different query! You could either use the same connection (bad) or create a new one and fire a different query(s).

Now whether each resource hits a different table or the same table with a different query depends on your choice of 'representation' for that resource. There is a reason a RDB schema has multiple tables and it's quite common you'll have a different query involving multiple tables or to mutually independent tables.

Performance issues: For 'fresh data' you ARE always going to hit the DB so to speak. If you want to optimize that either develop your own cache (extremely hard) or use approaches like memcached or ehcache to boost performance - before you decide to do that make sure you verify if it's worth it.

Are you going to be having about 1000 DB hits per second? You probably need some performance boosting/handling. Per day...maybe not. Per 2-3 days...YAGNI (You ain't gonna need it, so don't worry for now)

So, for every 'resource' that you design in your application (Login is NOT a resource: See related post: Why is form based authentication NOT considered RESTful?) choose the representation. It may involve different queries etc., for you to return json/xml/xhtml (whatever you choose). Each 'DB related call' should be isolated into it's own 'data layer' - I suggest go with Spring JDBC to make your life easier. It'll take the burden of JDBC plumbing off your shoulders so you can focus on creating your DAOs (Data Access Objects - a patter for Data Access classes. All DAOs logically belong in the data layer)

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文