繁体   English   中英

使用Web Service从数据库检索数据

[英]Retrieve data from database with Web Service

我有一个问题,我是新来的。 我知道如何创建从数据库中插入和删除的Web方法,但我不知道如何从数据库中获取某些信息。 我想我必须将查询的结果传递给一个字符串,然后返回此字符串才能得到我想要的。 我有以下不完整的代码。

@WebMethod(operationName = "getSomethingByID")
public String getSomethingByID(@WebParam(name = "idRocks")
String idRocks) {
    Dal dal = new Dal();
    ResultSet rs = dal.executeQuery("SELECT rocks FROM something WHERE idRocks ='" + idRocks+ "'");

        try
        {
            if(rs.next())
            {


            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }

        return null;
}

如何从数据库中检索此信息?

谢谢

您将需要以下内容:

String query = "SELECT rocks FROM something WHERE idSomething='" + idSomething+ "'";

Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306";
Connection conn = DriverManager.getConnection(url, "root", "root");
Statement stmt = conn.createStatement();

ResultSet rs;
rs = stmt.executeQuery(query);

String result = "";

while (rs.next()) {
    //'col1' refers to the value in each row in a column called col1 in you table
    result += rs.getString("col1");
}

这只是将值合并为一个长字符串,然后可以返回该值。

如果您使用的是这种方法,则需要导入JDBC Jar(假设它是您正在使用的MySQL数据库)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM