Browse Source

上传文件至 'src/helloPie'

龙泳贤 5 năm trước cách đây
mục cha
commit
c48c8d69f4
3 tập tin đã thay đổi với 157 bổ sung0 xóa
  1. 83 0
      src/helloPie/helloPie.java
  2. 18 0
      src/helloPie/pieBean.java
  3. 56 0
      src/helloPie/pieDAO.java

+ 83 - 0
src/helloPie/helloPie.java

@@ -0,0 +1,83 @@
+package helloPie;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import net.sf.json.JSONArray;
+
+
+public class helloPie extends HttpServlet {
+
+	/**
+	 * Constructor of the object.
+	 */
+	public helloPie() {
+		super();
+	}
+
+	/**
+	 * Destruction of the servlet. <br>
+	 */
+	public void destroy() {
+		super.destroy(); // Just puts "destroy" string in log
+		// Put your code here
+	}
+
+	/**
+	 * The doGet method of the servlet. <br>
+	 *
+	 * This method is called when a form has its tag value method equals to get.
+	 * 
+	 * @param request the request send by the client to the server
+	 * @param response the response send by the server to the client
+	 * @throws ServletException if an error occurred
+	 * @throws IOException if an error occurred
+	 */
+	public void doGet(HttpServletRequest request, HttpServletResponse response)
+			throws ServletException, IOException {
+
+		response.setContentType("text/html");
+		PrintWriter out = response.getWriter();
+		out.flush();
+		out.close();
+	}
+
+	/**
+	 * The doPost method of the servlet. <br>
+	 *
+	 * This method is called when a form has its tag value method equals to post.
+	 * 
+	 * @param request the request send by the client to the server
+	 * @param response the response send by the server to the client
+	 * @throws ServletException if an error occurred
+	 * @throws IOException if an error occurred
+	 */
+	public void doPost(HttpServletRequest request, HttpServletResponse response)
+			throws ServletException, IOException {
+        pieDAO bardao=new pieDAO();
+        ArrayList<pieBean> array = (ArrayList<pieBean>) bardao.listAll();
+        System.out.println("s"+array);
+        response.setContentType("text/html; charset=utf-8");
+        JSONArray json=JSONArray.fromObject(array);
+        System.out.println(json.toString());
+        PrintWriter out = response.getWriter();  
+        out.println(json);  
+        out.flush();  
+        out.close();   
+	}
+
+	/**
+	 * Initialization of the servlet. <br>
+	 *
+	 * @throws ServletException if an error occurs
+	 */
+	public void init() throws ServletException {
+	}
+
+}

+ 18 - 0
src/helloPie/pieBean.java

@@ -0,0 +1,18 @@
+package helloPie;
+
+public class pieBean {
+	 private String name;
+	    private float score;
+	    public String getName() {
+	        return name;
+	    }
+	    public void setName(String name) {
+	        this.name = name;
+	    }
+	    public float getScore() {
+	        return score;
+	    }
+	    public void setScore(float score) {
+	        this.score = score;
+	    }
+}

+ 56 - 0
src/helloPie/pieDAO.java

@@ -0,0 +1,56 @@
+package helloPie;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.List;
+
+public class pieDAO {
+	Connection connection;
+
+	public Connection getConnection() {
+		try {
+			String name = "root";
+			String password = "001011";
+			String url = "jdbc:mysql://localhost:3306/kui?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
+			Class.forName("com.mysql.jdbc.Driver");
+			connection = DriverManager.getConnection(url, name, password);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return connection;
+	}
+
+	public void setConnection(Connection connection) {
+		this.connection = connection;
+	}
+
+	public List<pieBean> listAll() {
+		ArrayList<pieBean> list = new ArrayList<pieBean>();
+		PreparedStatement pstmt = null;
+		ResultSet rs = null;
+		try {
+			pstmt = this.getConnection().prepareStatement("SELECT * FROM pie");
+			rs = pstmt.executeQuery();
+			while (rs.next()) {
+				pieBean pie = new pieBean();
+				pie.setName(rs.getString("name"));
+				pie.setScore(rs.getFloat("score"));
+				list.add(pie);
+				System.out.println("Á¬½ÓÊý¾Ý¿â³É¹¦");
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				connection.close();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		return list;
+	}
+
+}