将数据从RESTful Web服务发布到Microsoft Azure数据库

时间:2015-05-22 16:58:43

标签: java database azure post

我有以下代码将数据从Web服务POST到数据库。我正在使用RESTful Web服务,我正在连接到Microsoft Azure数据库。我在服务器运行模式下收到此错误消息 - >'资源不可用'。有人可以帮我解决这个问题吗?

@Path("/addcar/{CSID}/{PID}/{LicensePlateNumber}/{RegistrationNumber}/{CarModel}/{MakeYear}")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes (MediaType.APPLICATION_FORM_URLENCODED)

public String insertdata(@PathParam("CSID") String csid,
        @PathParam("PID") String pid,
        @PathParam("LicensePlateNumber") String lpn,
        @PathParam("RegistrationNumber") String rn,
        @PathParam("CarModel") String carmodel,
        @PathParam("MakeYear") String makeyear) throws ServletException,
        IOException, SQLException, ClassNotFoundException {
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement query = null;
        String myString = null;

        System.out.println(csid+ ","+ pid +","+lpn + ","+rn + ","+carmodel +","+ makeyear);

        //Car car = new Car();
        //car.newcar(csid, pid, lpn, rn, carmodel,makeyear);
           String DB_URL = "jdbc:sqlserver://nysj1rpznm.database.windows.net:1433;database=gparkdb;user=gpark@nysj1rpznm;password=Technology00000;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
           String USER = "gpark";
           String PASS = GlobalUtil.PASS; 
try{
          //STEP 2: Register JDBC driver
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);
            stmt = conn.createStatement();
            String sql = "INSERT into gpark_cars(CSID,PID,LicensePlateNumber,RegistrationNumber,CarModel,MakeYear) VALUES("+csid+""+ pid +""+ lpn+ ""+ rn+ ""+ carmodel+""+ makeyear+")";
            ResultSet rs2 = stmt.executeQuery(sql);
            System.out.println(sql);
            stmt.executeUpdate(sql);
            conn.close();
            stmt.close();
            conn.close();
}
catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
 }catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
 }finally{
    //finally block used to close resources
    try{
       if(stmt!=null)
          stmt.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
       if(conn!=null)
          conn.close();
    }catch(SQLException se){
       se.printStackTrace();
    }//end finally try
 }//end try
 return "Vehilce added";
 //System.out.println("Goodbye!");
}

0 个答案:

没有答案