Android手机客户端通过JSP实现与Tomcat服务器端通信(Msql数据库,Json作为载体)
--服务端代码
服务器端主要代码:
1.首先构建一个Person类,用来保存用户信息
[java] view plaincopy
public class Person
private String name;
private String address;
private Integer age;
public Person() {
super();
}
public Person(String name, String address, Integer age) {
super();
this.name = name;
this.address = address;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2.服务器端的service类
[java] view plaincopy
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;
public class JsonService {
public static List<Person> getListPerson(){
String a,b,allString = "";
int c = 0;
List<Person> mLists = new ArrayList<Person>();
// List<Person> mLists = new ArrayList<Person>();
// mLists.add(new Person("临风", "北京", 20));
System.out.println(mLists);
try {
Class.forName("com.mysql.jdbc.Driver");//链接数据库
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/json", "root" , "123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from people");//遍历数据库
Person p = null;
while(rs.next())
&nbs