我正在使用此代码创建一个登录Web服务到数据库
public class classeConnection {
@GET
@Path (value="log/{a}/{b}")
@Produces(MediaType.APPLICATION_JSON)
public String execMAJ(@PathParam("a")String name,@PathParam("b")String lastname )throws SQLException {
String req;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e ){};
Connection cn= DriverManager.getConnection("jdbc:mysql://localhost:3306/codezone4", "root","");
Statement st=cn.createStatement();
req="select id from utilisateur where nom = '+name+' and prenom = '+lastname+' ";
st.close();
cn.close();
System.out.println("success");
return "login";
}
}
我的代码正在运行,但我不知道如何提取sql请求的结果(此处为id) 对这个问题有什么建议吗?请帮忙!
答案 0 :(得分:1)
public class classeConnection {
@GET
@Path (value="log/{a}/{b}")
@Produces(MediaType.APPLICATION_JSON)
public String execMAJ(@PathParam("a")String name,@PathParam("b")String lastname )throws SQLException {
String req;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e ){
e.printStackTrace();
}
try {
Connection cn= DriverManager.getConnection("jdbc:mysql://localhost:3306/codezone4", "root","");
Statement st=cn.createStatement();
req="select id from utilisateur where nom = "+name+" and prenom = "+lastname+" LIMIT 1";
ResultSet res = st.executeQuery(req);
if(res.first()) {
String id = res.getString("id");
System.out.println("id = "+id);
} else {
System.out.println("not found foo!");
}
}
catch (SQLException s){
s.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
finally {
st.close();
cn.close();
}
return "login";
}
}