尝试连接到数据库时Web服务错误

时间:2014-02-11 03:37:43

标签: android mysql database eclipse web-services

最近我正在搜索如何在eclipse中创建一个Web服务来创建一个简单的登录页面,我创建了一个Web服务,但我在这里遇到的错误是当我调用此Web服务以使其从中检索密码和用户名时数据库它给出了一个未知的错误我搜索得很好,我什么也没找到,所以请帮助我被困在这里。

这是错误:

 [ERROR] 2
 java.lang.ArrayIndexOutOfBoundsException: 2
at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:630)
at org.apache.axis2.rpc.receivers.RPCUtil.processRequest(RPCUtil.java:153)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:206)
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

这是代码:

package com.abod.abd;

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

public class calculate {
    public String authentication(String userName,String password){

        String retrievedUserName = "";
        String retrievedPassword = "";
        String status = "";
        try{

            Class.forName("com.mysql.jdbc.Driver");
            Connection con =             DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","aa198965");
            PreparedStatement statement =  con.prepareStatement("SELECT * FROM `user`");

            ResultSet result = statement.executeQuery();

            while(result.next()){
                retrievedUserName = result.getString("username");
                retrievedPassword = result.getString("password");
            }

            if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)){
                status = "Success!";
            }

            else{
                status = "Login fail!!!";
            }

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

3 个答案:

答案 0 :(得分:1)

我终于找到了问题,它是在android端我更改了参数以匹配webservice并且每个工作都找不到

答案 1 :(得分:0)

Android无法理解localhost。使用10.0.2.2代替localhost并再试一次。

答案 2 :(得分:0)

如果您想从Android连接到MySQL数据库,您只需按照以下步骤操作:

  • 下载驱动程序mysql-connector-java-3.0.17-ga-bin.jar
  • 将其粘贴到Android项目中的文件夹libs。
  • 单击ConfigureBuildPath->添加jar以将jar包含到项目中。
  • 检查AndroidManifest.xml中的INTERNET权限。

使用下一个代码进行连接:

try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e){
    System.err.println("Cannot create connection");
}
try{
    connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname","root","password");
    Statement statement = connection.createStatement();

    String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
    query = query +"'" +variable+"'";           
    ResultSet result = statement.executeQuery(query);
}catch(Exception e){
    System.err.println("Error");
}

建议:如果驱动程序的实例没有给出任何错误,但是你得到连接异常,你应该尝试从清单中删除Target SDK版本,对于某些版本,这会产生问题。

参考:https://stackoverflow.com/a/14982316/1318946