Java教程

Java中的异常错误汇总

本文主要是介绍Java中的异常错误汇总,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

1.使用rmi测试发生的异常

2.Mybatis执行sql语句参数错误


1.使用rmi测试发生的异常

java.rmi.server.ExportException: remote object implements illegal remote interface; nested exception is: 
	java.lang.IllegalArgumentException: illegal remote method encountered: public abstract java.lang.String com.righteye.remote.IHello.sayHello(java.lang.String)
	at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:228)
	at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:383)
	at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:320)
	at java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:198)
	at java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:180)
	at com.righteye.remote.impl.IHelloImpl.<init>(IHelloImpl.java:10)
	at com.righteye.remote.impl.RmiServer.main(RmiServer.java:15)
Caused by: java.lang.IllegalArgumentException: illegal remote method encountered: public abstract java.lang.String com.righteye.remote.IHello.sayHello(java.lang.String)
	at sun.rmi.server.Util.checkMethod(Util.java:267)
	at sun.rmi.server.Util.getRemoteInterfaces(Util.java:246)
	at sun.rmi.server.Util.getRemoteInterfaces(Util.java:216)
	at sun.rmi.server.Util.createProxy(Util.java:146)
	at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:226)
	... 6 more

解决方案:

注册远程调用的服务需要 throws RemoteException

public interface IHello extends Remote {

    public String sayHello(String name) throws RemoteException;
}

public class IHelloImpl extends UnicastRemoteObject implements IHello {
    protected IHelloImpl() throws RemoteException {
        super();
    }

    @Override
    public String sayHello(String name) throws RemoteException {

        return "Hello" + name;
    }
}

2.Mybatis执行sql语句参数错误

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'myname' not found. Available parameters are [arg1, arg0, param1, param2]
### The error may exist in com/righteye/day01/dao/UserDao.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert into user(username, password) values(?, ?)
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'myname' not found. Available parameters are [arg1, arg0, param1, param2]

        解决方案:参数名不匹配问题,当传入多个参数的时候,可以使用对象的形式进行传参,使用对象属性名进行赋值;如果使用基本数据类型,可以在接口方法声明的参数前加上Param(自定义参数名) 给参数起别名

 int insertUser(@Param("myname")String username, @Param("pwd")String password);
    <insert id="insertUser">
        insert into user(username, password) values(#{myname}, #{pwd})
    </insert>

这篇关于Java中的异常错误汇总的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!