我正在使用Java使用数据库的RESTful Webservice。通过在Netbeans中使用来自数据库的RESTful Webservice选项,它会生成一些类,以便我们可以公开像count,{id},{from}/{id}这样的服务。Java RESTful Webservice使用Netbeans IDE的CRUD Opreation
我们如何编写程序插入,使用Java Netbeans中删除并更新。
这是我的工作环境。
我正在使用Java使用数据库的RESTful Webservice。通过在Netbeans中使用来自数据库的RESTful Webservice选项,它会生成一些类,以便我们可以公开像count,{id},{from}/{id}这样的服务。Java RESTful Webservice使用Netbeans IDE的CRUD Opreation
我们如何编写程序插入,使用Java Netbeans中删除并更新。
这是我的工作环境。
插入代码类似如下:
@POST
@Path("insertion")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String register(@FormParam("passhash") String passhash, @FormParam("email") String email,@FormParam("$pswdhash") String pwd, @FormParam("phno") String phno) {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/test", "app", "app");
PreparedStatement pst = con.prepareStatement("insert into MMX_REGISTRATION(name,email,pswd,phno) values(?,?,?,?)");
pst.setString(1, passhash);
pst.setString(2, email);
pst.setString(3, pwd);
pst.setString(4, phno);
int result = pst.executeUpdate();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
return "success";
//return "listform.html";
}
检索数据如下:
@Context private HttpServletRequest request;
@GET
@Path("session")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String session(@QueryParam("lname") String name1) {
String response2 = null;
//String name11 = "praveen";
//String a[] = null;
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/test", "app", "app");
//PreparedStatement pst = con.prepareStatement("insert into restdb_insertion(id,company) values(?,?)");
//String str1="select * from restdb_insertion where registration=?";
PreparedStatement pst = con.prepareStatement("select * from MMX_REGISTRATION where name='"+name1+"'");
System.out.println("select * from MMX_REGISTRATION where name='"+name1+"'");
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next()) {
if(!"null".equals(rs.getString(1))){
request.getSession(true);
HttpSession session = request.getSession();
session.setAttribute("name","value");
session.setAttribute("UserName", rs.getString(2));
String username = (String)session.getAttribute("UserName");
System.out.println(username);
// System.out.println(name);
//request.getSession(false);
//request.getSession().invalidate();
//String user = (String)session.getAttribute("UserName");
//System.out.println(user);
return "success"+" "+username;
}
}
//response = name1;
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
//"<rss version='2.0'><channel><id>" + id + "</id><cmp>" +response.toArray()[0] + "</cmp></channel></rss>"
}
这个问题是有用 –
尽量保持DB互动在不同的层面上......这是非常紧密耦合的 –
** AbstractFacade **确实具有所有这些方法。你让自己变得复杂了,这不是一个好习惯。如果你有多少个实体,你会怎么做? –
独立工作分为两层DAO和服务
注入你的DAO实现的依赖关系到你的Web服务层和调用你的CRUD操作(这是EJB概念,你也可以尝试春季)
也许你应该采取看春,数据...你只是有一个Maven的导入,界面与所有你需要的方法,它会使用你的名称的方法提出要求......
你应该定义你所有的CRUD操作一个安静的班级。在每个宁静类的方法中,你应该调用一个服务接口方法,该方法有另一个类,它的实现就是ServiceImpl。您的服务的每种方法Impl都应该与Dao层交互以进行CRUD操作。 你应该避免一次又一次地加载驱动程序类为每个CRUD操作,你应该在这样一个单独的方法/静态块定义它: -
static Connection con;
static{
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
con = DriverManager.getConnection("jdbc:derby://localhost:1527/test", "app", "app");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
有这个这么多的教程,如果你谷歌“REST CRUD netbeans“示例http://www.madebyjohann.com/index.php/college/learn-how/57-crud-in-a-restful-api-with-netbeans –
@ring持票人我访问了许多他们设计的网站示例Helloword程序使用RESTful Webservice,或者他们展示了基本代码,即由IDE生成但不是CRUD操作。 –
@ring持票人我遵循此链接http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices/RESTfulWebservices.htm#t2其中描述示例数据库连接并自动从数据库选项从RESTful Webservice生成代码选项来自Netbeans –