我有一个连接到mySQL数据库的soap Web服务。它有两个方法,一个是insert(),另一个是verify()。第一个允许用户输入名称,电子邮件和密码等数据。它成功地将记录保存在数据库中。
但是我在编写验证方法时遇到了问题。它有输入参数,电子邮件和密码。必须将输入的数据与存储在数据库中的数据进行比较,如果无法在mysql数据库中找到匹配项,则返回“已注册”或“未注册”。我在编写代码时遇到了问题。你能帮忙吗,我是jdbc和java web服务的新手。我在使用netbeans。 非常感谢。
以下是我的代码:
@WebMethod(operationName = "insert")
public String insert(@WebParam(name = "name") String name,
@WebParam(name = "email") String email,
@WebParam(name = "password") String password {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "1234");
PreparedStatement st = con.prepareStatement("insert into register values(?,?,?)");
st.setString(1, name);
st.setString(2, email);
st.setString(3, password);
st.executeUpdate();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return "record inserted";
}
/**
* Web service operation
*/
@WebMethod(operationName = "Verify")
public String CheckUser(@WebParam(name = "email") String email,
@WebParam(name = "password") String password) {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "1234");
If
// problems to write this statement!!! I need to compare the username and password
// with some select * from register where password == @password and email == @email?
return "Registered user";
else
return "Not registered";
}
答案 0 :(得分:0)
检查可能对您有用的网址
http://www.roseindia.net/jdbc/jdbc-mysql/SelectRecords.shtml
答案 1 :(得分:0)
此示例可行(您可能必须更改查询中的列名)。此外,您应该以与此示例相同的方式关闭insert方法中的连接。
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "1234");
PreparedStatement st = con.prepareStatement("select 1 from register where password = ? and email = ? and name = ?");
st.setString(1, password);
st.setString(2, email);
st.setString(3, name);
ResultSet result = st.executeQuery();
if (result.next()) { //.next() returns true if there is a next row returned by the query.
return "Registered user";
} else {
return "Not registered";
}
} catch (Exception e) {
//TODO manage the exception.
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {}
}
}