REST服务器连接到数据库并获取数据

时间:2014-08-28 18:42:55

标签: java json eclipse rest netbeans-6.9

我是JAVA和网络服务的新手。

我想在REST客户端JAVA桌面应用程序中调用一个方法。客户端在NetBeans IDE中,它应该类似于框架,它调用REST服务器(动态Web项目),Server在Eclipse IDE中,它也类似于框架 - 可以根据用户需要轻松更改。服务器应连接到MS SQL数据库以获取数据并将该数据传递到客户端,并在客户端桌面应用程序的相关文本框中显示这些值。 REST客户端代码以获取JTextField值。

 String id = this.jTextField1.getText();
 ObjectMapper mapper = new ObjectMapper();
        try {
              mapper.writerWithDefaultPrettyPrinter().writeValueAsString(id);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(SearchOne_1.class.getName()).log(Level.SEVERE, null, ex);
        }

当我单击该按钮时,我想将该文本字段值传递给REST服务器。 那部分代码尚未实现,仍在寻找方法。

REST服务器代码是这样的。 这是SQLtask.java。这适用于选择查询。

    public SQLTasks(String dbSource) {
    super(dbSource);
}
public void SQLSelect(String SQL_SELECT) throws SQLException {

  try{
    stmt = conn.createStatement();  //connection created in MDBConnection class
    rs = stmt.executeQuery(SQL_SELECT);
  }
  catch(SQLException sqEx){
       System.out.println("SQL_EX "+sqEx);
       System.out.println("SQL_EX SELECT "+SQL_SELECT);
       throw new SQLException();

  }catch(Exception sqEx){
       System.out.println("EXC "+sqEx);
       System.out.println("EXC SELECT "+SQL_SELECT);
       throw new SQLException();
  }
}

此代码适用于DBAccess.java

public Employee getEmployeeDataByEPFNo(String dataSource,String EPFNo){
    SQLTasks st1 = new SQLTasks(dataSource);
    Employee employee=new Employee();
    try {
        st1.SQLSelect("SELECT * FROM Employee WHERE id="+EPFNo);
           ResultSet rs1 = st1.rs;
            while (rs1.next()) {


                employee.setEpfno(rs1.getString("EPFNo"));
                employee.setEfname(rs1.getString("fName"));
                employee.setElname(rs1.getString("lName"));
                employee.setDesignation(rs1.getString("designation"));
                employee.setNic(rs1.getString("NIC"));
                employee.setAddress(rs1.getString("Address"));
                employee.setArrival(rs1.getString("eArrivalDate"));
                employee.setDob(rs1.getString("DOB"));

            }
    } catch (SQLException e) {

        e.printStackTrace();
    }catch (Exception e) {

        e.printStackTrace();
    }
 return employee;
}

这是RESTServer.java

@GET
 @Path("/dbAccess/getDBValEmpByEPF/{EPFNo}")
 @Produces(MediaType.APPLICATION_JSON)

 public Employee getDBValEmpByEPF(@PathParam("EPFNo") String EPFNo) {
     Employee e= new Employee();
    DBAccess dbAcc=new DBAccess();
    e= dbAcc.getEmployeeDataByEPFNo("jdbc/db_sample_mssql","EPFNo");//call getEmployeeDataByEPFNo() which is inside DBAccess class
    return e;


    }

而是手工输入值为" 500",我想要的是传递从客户端获得的价值。可能吗 ?

1 个答案:

答案 0 :(得分:0)

您已在休息服务中使用 @PathParam 作为EPFNo,因此要获得该值,您需要在请求网址中传递EPFNo的值,如下所述

http://localhost:8080/TestRestWebService/appPath/dbAccess/getDBValEmpByEPF/3245

这里 3245 是我们从客户端传入服务器的EPFNo值,并且在getDBValEmpByEPF()方法中可以使用。

@GET
@Path("/dbAccess/getDBValEmpByEPF/{EPFNo}")
public Response getDBValEmpByEPF(@PathParam("EPFNo") String EPFNo) {
    System.out.println("EPFNo is="+EPFNo);//it will print EPFNo is=3245
       ......}

示例客户端代码

try {
        String EPFNo="234";//JTextField field value
        URL url = new URL(
                "http://localhost:8080/TestRestWebService/appPath/dbAccess/getDBValEmpByEPF/"+EPFNo);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));
        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

希望这会对你有所帮助。