oracle 使用 Web 服务从数据库中检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5797549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve data from database with Web Service
提问by thathashd
I have a problem and i am new to this. I know how to create web methods to insert and delet from database but i don't know how to get something from the database. I guess i have to pass the result of the query to a string and then return this string in order to get what i want. I have the following incomplete code.
我有一个问题,我是新手。我知道如何创建 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;
}
How can i retrieve this information from database?
如何从数据库中检索此信息?
thanks
谢谢
采纳答案by travega
you will need something like:
你将需要类似的东西:
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");
}
This just concatinates the values into one long string and you can then return that value.
这只是将值连接成一个长字符串,然后您可以返回该值。
You will need to import a JDBC Jar if you are using this approach (assuming it is a MySQL db you are using.)
如果您使用这种方法,则需要导入 JDBC Jar(假设它是您正在使用的 MySQL 数据库。)