0

我正在尝试构建一个简单的 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());





}

}

4

0 回答 0