问题描述
我想把一些数值由客服端发送到服务端,然后服务端把收到的数值写进数据库.我用Axis2做了webservice和客服端,还有连接数据库的代码我想问当我运行客户端后,服务端的Person[]是不是会改变了数值还有我该把操作数据库的代码放在哪里?我知道我什么都不懂,我知道我需要学习,但也希望有人指点.服务端:packagesample.ws.pojo;publicclassPerson{privateStringname;privateintage;publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}///////////////////////////////////packagesample.ws.service;importsample.ws.pojo.Person;publicclassServiceTest{/***很简单我们只是将发过来的person[]返回去*@paramperson*@return*/publicPerson[]getPerson(Person[]person){Personperson1=newPerson();Personperson2=newPerson();Person[]person3=newPerson[2];returnperson;}}/////////////////////////////////客户端:packagesample.ws.service;publicclassPerson{privateStringname;privateintage;publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}/////////////////////////////////////////packagesample.ws.service;importjavax.xml.namespace.QName;importorg.apache.axis2.addressing.EndpointReference;importorg.apache.axis2.client.Options;importorg.apache.axis2.rpc.client.RPCServiceClient;importsample.ws.service.Person;publicclassTTSS{publicstaticvoidmain(Stringargs[])throwsjava.lang.Exception{RPCServiceClientclient=newRPCServiceClient();Optionsoption=client.getOptions();//指定客户端访问的webservice服务器端地址EndpointReferenceerf=newEndpointReference("http://localhost:8080/Ws2Pojo/services/ServiceTest?wsdl");option.setTo(erf);//指定命名空间,指定要调用的方法QNamename=newQName("http://service.ws.sample","getPerson");//创建Person对象Personperson1=newPerson();person1.setAge(20);person1.setName("张三");Personperson2=newPerson();person2.setAge(30);person2.setName("李四");//创建Person数组Person[]person3=newPerson[2];person3[0]=person1;person3[1]=person2;//创建要传送的object数组Object[]object=newObject[]{person3};//创建返回的参数类型Class[]returnTypes=newClass[]{Person[].class};//调用远程服务,得到返回的object数组Object[]response=client.invokeBlocking(name,object,returnTypes);//强制转换成Person[]对象Person[]p=(Person[])response[0];//遍历得到我们刚刚请求过去的值for(inti=0;i<p.length;i++){System.out.println(p[i].getAge());System.out.println(p[i].getName());}//stub.getPerson(getPerson0);//System.out.println(stub.getPerson(getPerson0));}}/////////////////////////////////////连接数据库:packagesample.ws.pojo;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassDbCon{publicstaticConnectiongetConnection()throwsException{Class.forName("com.mysql.jdbc.Driver");returnDriverManager.getConnection("jdbc:mysql://localhost:3306/ejvas","root","root");}publicstaticvoidmain(String[]args)throwsException{Personaa=newPerson();Stringkkk="INSERTINTOpersonVALUES('";Stringll="','lishi')";System.out.println(aa.getName());Connectionconn=null;Statementstmt=null;ResultSetrs=null;try{conn=getConnection();stmt=conn.createStatement();conn.setAutoCommit(false);//stmt.executeUpdate("CREATETABLEperson"+"(ageINT,nameVARCHAR(30))");stmt.executeUpdate("INSERTINTOpersonVALUES('20','zhangsan')");stmt.executeUpdate(kkk+aa.getName()+ll);conn.commit();conn.setAutoCommit(true);rs=stmt.executeQuery("SELECTname,ageFROMperson");while(rs.next()){Stringname=rs.getString(1);//1stcolumninqueryStringage=rs.getString(2);//2ndcolumninquerySystem.out.println("name="+name);System.out.println("age="+age);System.out.println("==========");}}catch(ClassNotFoundExceptionce){System.out.println(ce.getMessage());}catch(SQLExceptione){if(conn!=null){conn.rollback();conn.setAutoCommit(true);}System.out.println("---SQLExceptioncaught---");while(e!=null){System.out.println("Message:"+e.getMessage());System.out.println("SQLState:"+e.getSQLState());System.out.println("ErrorCode:"+e.getErrorCode());System.out.println("---");e=e.getNextException();}}finally{try{rs.close();stmt.close();conn.close();}catch(Exceptione){}}}}