不知道哪里错了,该怎么改,求各位大神教!谢谢!
报错: Can not issue data manipulation statements with executeQuery().
代码如下:
public class person_entity_dao {
public List quaryList(String name,String age,String sex,String time,String telphone,String rid,String did,String username,String password){
Map<String, String> map =new HashMap<String, String>();
ResultSetMetaData md = null;
List listOfRows = new ArrayList();
try {
Connection conn=this.connectionDB();
String insert="insert into tb_user (name,age,sex,time,telphone,rid,did,username,password) values ('"+name+"','"+age+"','"+sex+"','"+time+"','"+telphone+"','"+rid+"','"+did+"','"+username+"','"+password+"')";
System.out.println(insert);
//编译sql语句
PreparedStatement ps=conn.prepareStatement(insert);
//获得结果集
ResultSet rs= ps.executeQuery();
md=rs.getMetaData();
int num=md.getColumnCount();
while(rs.next()){
LinkedHashMap mapOfColValues = new LinkedHashMap(num);
for(int i=0;i<=num;i++){
mapOfColValues.put(md.getColumnName(i), rs.getObject(i));
}
listOfRows.add(mapOfColValues);
}
} catch (Exception e) {
e.printStackTrace();
}
return listOfRows;
}
public Connection connectionDB() throws Exception{
String url="jdbc:mysql://localhost:3306/test";
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url, "root", "123456");
}
catch(ClassNotFoundException e){
System.out.println("驱动失败");
}
catch(SQLException e){
System.out.println("连接数据库失败");
}
return conn;
}
public class person_service {
person_entity_dao dao =new person_entity_dao();
public List quaryList(String name,String age,String sex,String time,String telphone,String rid,String did,String username,String password){
return dao.quaryList(name, age, sex, time, telphone, rid, did, username, password);
}
}
public class personAction extends ActionSupport {
private static final long serialVersionUID = 1L;
person_service service =new person_service();
public String insert() throws Exception {
String name=ServletActionContext.getRequest().getParameter("name");
String age=ServletActionContext.getRequest().getParameter("age");
String sex=ServletActionContext.getRequest().getParameter("sex");
String time=ServletActionContext.getRequest().getParameter("time");
String telphone=ServletActionContext.getRequest().getParameter("telphone");
String rid=ServletActionContext.getRequest().getParameter("rid");
String did=ServletActionContext.getRequest().getParameter("did");
String username=ServletActionContext.getRequest().getParameter("username");
String password=ServletActionContext.getRequest().getParameter("password");
List listofRows=service.quaryList(name, age, sex, time, telphone, rid, did, username, password);
System.out.println(listofRows.toString());
//双向搜索接口Iterator
Iterator its=listofRows.iterator();
while( its.next()!= null){
return "success";
}
return "fail";
}
}