java - 服务类 "WBSer_RwCnt.Rw_Count"不符合 JAX-RPC 1.1 规范的一项或多项要求

标签 java web-services

package WBSer_RwCnt;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Rw_Count {

  public static Connection getConnection() throws Exception {

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost/hospital_data";
    String username = "root";
    String password = "mysql";

    Class.forName(driver); // load MySQL driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static int countRows(Connection conn, String tableName) throws SQLException {

    // select the number of rows in the table
    Statement stmt = null;
    ResultSet rs = null;
    int rowCount = -1;
    try {
      stmt = conn.createStatement();
      rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
      // get the number of rows from the result set
      rs.next();
      rowCount = rs.getInt(1);
    } finally {
      rs.close();
      stmt.close();
    }
    return rowCount;
  }

  public static void main(String[] args) {

        Connection conn = null;
    try {
      conn = getConnection();
      String tableName = "hospital_status";
      System.out.println("tableName=" + tableName);
      System.out.println("conn=" + conn);
      System.out.println("rowCount=" + countRows(conn, tableName));
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    } finally {
      // release database resources
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

错误 --->

The method "getConnection" on the service class "WBSer_RwCnt.Rw_Count" uses a data type, "java.sql.Connection", that is not supported 

When i compile it without creating it as webservice it works correctly but when i make it as web service it gives output as

Output --->

WBSer_RwCnt.Rw_CountSoapBindingStub@121a412b

Please Help !

Next Try

So this is what i have done after what you have said even then it gives following errors

Exception:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/hospital_data

Message:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/hospital_data
package WBSer_RwCnt;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Rw_Count {

public static int countRows() throws SQLException {

// select the number of rows in the table

Statement stmt = null;

ResultSet rs = null;

System.out.println("ram");

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://localhost/hospital_data";

String username = "root";

String password = "mysql";

try {

    Class.forName(driver);

}
 catch (ClassNotFoundException e1)
 {

    // TODO Auto-generated catch block

    e1.printStackTrace();

}

// load MySQL driver

Connection conn = DriverManager.getConnection(url, username, password);

int rowCount = -1;

try {

  stmt = conn.createStatement();

  rs = stmt.executeQuery("SELECT COUNT(*) FROM hospital_status");

  // get the number of rows from the result set

  rs.next();

  rowCount = rs.getInt(1);

}

catch (Exception e) 

{
    e.printStackTrace();

    System.exit(1);

  }

finally {

  rs.close();

  stmt.close();

  conn.close();

}

return rowCount;

}

}

我添加了所有 jar 文件,包括 java mysql 连接器

最佳答案

这不会是一个完整的答案,因为我不太确定您用来编译 Web 服务的工具,但无论如何,这里是:

基本上,连接是仅在特定机器上有效的东西。如果是 TCP/IP 连接,则由两对组成:源主机和端口,以及目标主机和端口。如果它是 Linux 套接字,那么它就是该特定计算机的目录树中的一个条目。

数据库连接通常建立在其中一个构造之上,因此它也是特定于机器的。

因此,将 Connection 对象传递给从远程计算机调用您的方法的用户是没有意义的。由于它没有意义,JAX-RPC 标准不包含 Connection 的序列化,这就是它失败的原因。

您的问题是您设计的方法使其接受连接作为参数,并使用该连接访问数据库。这在本地工作正常,但对于远程服务来说不是一个好的设计。

相反,您的方法应该在内部获取连接。远程用户应仅使用表名称访问 countRows 方法,并且 countRows 应调用 getConnection、使用连接,并且关闭它。

Web 服务中不应该有 main 方法。并且 getConnection 方法应从 public 更改为 private,以便 countRows 可以访问它。当它是私有(private)的时,我相信 Web 服务编译器不会提示它,因为它不必为其创建序列化。

关于java - 服务类 "WBSer_RwCnt.Rw_Count"不符合 JAX-RPC 1.1 规范的一项或多项要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27580004/

相关文章:

java - 使用 Chromedriver 在 Chrome 中无法使用热键(快捷方式)

Web 服务和单例 WCF 服务的 WCF 生命周期?

java - 用不同的值替换长字符串的一部分

java - 构建DOM文档对象时文件过早结束错误

未设置 PHP cURL Content-Type

web-services - REST 最佳实践 : Should you return an entity on POST and PUT calls?

c# - 如何查看谁使用了我的网络服务

c# - Windows 服务中托管的 WCF 服务中的 "No Endpoint Listening"

java - android.text.Editable android.widget.EditText.getText() 在应用程序点击按钮

java - 使用 JPA 和自动生成的主键标识将实体持久保存到数据库