在服务器上运行java Web服务

时间:2013-10-09 06:35:35

标签: java android mysql web-services jdbc

我有一个Java Webservice代码。目前我在localhost上运行它。当我在另一台服务器(使用静态IP)上运行时,应该进行哪些更改。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

public class RetailerWS {
 public String customerData(){
  String customerInfo = "";
  try{
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/retailer","root","chathura");
  //Find customer information where the customer ID is maximum
  PreparedStatement statement =  con.prepareStatement("SELECT * FROM customers WHERE C_ID = (SELECT MAX(C_ID) FROM customers)");
  ResultSet result = statement.executeQuery();

   while(result.next()){
    customerInfo = customerInfo + result.getString("name") + "&" + result.getString("C_ID") + "&"+result.getString("address") + "&"+result.getString("email");
  //Here "&"s are added to the return string. This is help to split the string in Android application
   }
  }

  catch(Exception exc){
   System.out.println(exc.getMessage());
   }

  return customerInfo;
  }

}

1 个答案:

答案 0 :(得分:0)

如果数据库也托管在同一台服务器上,则无需进行任何更改。如果DB在其他服务器上,则更改此行:

 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/retailer","root","chathura");

获取数据库服务器的IP /地址或主机名。

您的网络服务客户端需要使用您托管网络服务的新服务器的地址来调用其方法。