通过JQUERY和JASON进行前后的传递。
首先是进行servlet的创建,在数据库中获取1号和6号大棚的各项参数。然后封装为JASON格式发送给前端
package mysqlLogin.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
/**
* Servlet implementation class Login
*/
@WebServlet("/Home")
public class Home extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Home() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String driverMysql = "com.mysql.jdbc.Driver";
String urlMysql = "jdbc:mysql://localhost:3306/emp?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String userNameMysql = "root";
String passWordMysql = "asdf1234";
String sql1 = "select * from `emp`.`greenhouse` "+ "where houseid = '0001'" +" order by id desc limit 0,1 ";
String sql2 = "select * from `emp`.`greenhouse` "+ "where houseid = '0006'" +" order by id desc limit 0,1 ";
ArrayList<GreenHouse> greenList = new ArrayList<GreenHouse>();
String gsObject = null;
boolean t = false;
try {
Class.forName(driverMysql);
Connection conn = DriverManager.getConnection(urlMysql, userNameMysql, passWordMysql);
PreparedStatement ps = conn.prepareStatement(sql1);
ResultSet rs = ps.executeQuery();
rs.next();
//�¶�
String temp1 = rs.getString("temp");
//ʪ��
String humity1 = rs.getString("humity");
//����
String bright1 = rs.getString("bright");
//smoke
String smoke1 = rs.getString("smoke");
//co2
String co21 = rs.getString("co2");
//planttype
String planttype1 = rs.getString("planttype");
//ʱ��
String time1 = rs.getString("time");
//��
String secondIot1 = rs.getString("secondIot");
//������
String houseid1 = rs.getString("houseid");
GreenHouse gh1 = new GreenHouse(temp1,humity1,bright1,smoke1,co21,planttype1, time1, secondIot1, houseid1);
PreparedStatement ps2 = conn.prepareStatement(sql2);
ResultSet rs2 = ps2.executeQuery();
rs2.next();
//�¶�
String temp2 = rs2.getString("temp");
//ʪ��
String humity2 = rs2.getString("humity");
//����
String bright2 = rs2.getString("bright");
//smoke
String smoke2 = rs2.getString("smoke");
//co2
String co22 = rs2.getString("co2");
//planttype
String planttype2 = rs2.getString("planttype");
//ʱ��
String time2 = rs2.getString("time");
//��
String secondIot2 = rs2.getString("secondIot");
//������
String houseid2 = rs2.getString("houseid");
GreenHouse gh2 = new GreenHouse(temp2,humity2,bright2,smoke2,co22,planttype2, time2, secondIot2, houseid2);
greenList.add(gh1);
greenList.add(gh2);
Gson gs = new Gson();
gsObject = gs.toJson(greenList);
rs.close();
rs2.close();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8 ");
response.getWriter().print(gsObject);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package mysqlLogin.servlet;
public class GreenHouse {
//�¶�
String temp;
//ʪ��
String humity;
//����
String bright;
//smoke
String smoke;
//co2
String co2;
//planttype
String planttype;
//ʱ��
String time;
//��
String secondIot;
//������
String houseid ;
public GreenHouse(String temp, String humity, String bright, String smoke, String co2, String planttype,
String time, String secondIot, String houseid) {
super();
this.temp = temp;
this.humity = humity;
this.bright = bright;
this.smoke = smoke;
this.co2 = co2;
this.planttype = planttype;
this.time = time;
this.secondIot = secondIot;
this.houseid = houseid;
}
public GreenHouse(String temp, String humity, String time, String secondIot, String houseid) {
super();
this.temp = temp;
this.humity = humity;
this.time = time;
this.secondIot = secondIot;
this.houseid = houseid;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getHumity() {
return humity;
}
public void setHumity(String humity) {
this.humity = humity;
}
public String getBright() {
return bright;
}
public void setBright(String bright) {
this.bright = bright;
}
public String getSmoke() {
return smoke;
}
public void setSmoke(String smoke) {
this.smoke = smoke;
}
public String getCo2() {
return co2;
}
public void setCo2(String co2) {
this.co2 = co2;
}
public String getPlanttype() {
return planttype;
}
public void setPlanttype(String planttype) {
this.planttype = planttype;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSecondIot() {
return secondIot;
}
public void setSecondIot(String secondIot) {
this.secondIot = secondIot;
}
public String getHouseid() {
return houseid;
}
public void setHouseid(String houseid) {
this.houseid = houseid;
}
@Override
public String toString() {
return "GreenHouse [temp=" + temp + ", humity=" + humity + ", bright=" + bright + ", houseid=" + houseid
+ "]";
}
}