在webservice中使用JSON时的异常

时间:2012-05-26 13:41:36

标签: java json web-services

我正在尝试构建一个简单的Web服务来连接数据库,检索数据,将它们放在json对象中并发送一个代表该对象的json字符串 这是代码

我正在使用eclipse webservice向导,在测试客户端时,我得到以下异常 异常:java.lang.reflect.InvocationTargetException消息:java.lang.reflect.InvocationTargetException 当我删除和JSON对象声明时,异常消失 这是我的代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



import org.json.JSONArray;
import org.json.JSONObject;


public class Services {

    public String login(String email,String passwd){
    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DATABASE_URL = "jdbc:mysql://localhost/university";
    Connection connection;
    Statement statement;
    ResultSet resSet;
    //JSONArray resultarr=new JSONArray();
    JSONObject result=new JSONObject();

    boolean gotresult=false;
    try {
        Class.forName(JDBC_DRIVER);
        connection=DriverManager.getConnection(DATABASE_URL,"root","");
        statement=connection.createStatement();
        resSet=statement.executeQuery("select * from users where email='"+email+"' and pass='"+passwd+"'");
        while (resSet.next()){
            String fname=resSet.getString(3);
            String lname=resSet.getString(4);
            int rank=resSet.getInt(5);
            int id=resSet.getInt(1);
            result.put("id", id);
            result.put("fname", fname);
            result.put("lname", lname);
            result.put("rank", rank);

            gotresult=true;

        }
        if (!gotresult){
            result.put("error", "user not found");


        } 

    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return (result.toString());





}

}

0 个答案:

没有答案