Ver Fonte

上传文件至 'src/helloLine'

龙泳贤 há 5 anos atrás
pai
commit
374ec899ae

+ 94 - 0
src/helloLine/helloLine.java

@@ -0,0 +1,94 @@
+package helloLine;
+
+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 helloLine extends HttpServlet {
+
+	/**
+	 * Constructor of the object.
+	 */
+	public helloLine() {
+		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 {
+        lineDAO linedao=new lineDAO();
+        ArrayList<lineBean> array = (ArrayList<lineBean>) linedao.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();
+        
+//        lineDAO2 linedao2=new lineDAO2();
+//        ArrayList<lineBean> array2 = (ArrayList<lineBean>) linedao2.listAll();
+//        System.out.println("s"+array2);
+//        response.setContentType("text/html; charset=utf-8");
+//        JSONArray json2=JSONArray.fromObject(array2);
+//        System.out.println(json2.toString());
+//        PrintWriter out2 = response.getWriter();  
+//        out2.println(json2);  
+//        out2.flush();  
+//        out2.close();
+	}
+
+	/**
+	 * Initialization of the servlet. <br>
+	 *
+	 * @throws ServletException if an error occurs
+	 */
+	public void init() throws ServletException {
+	}
+
+}

+ 94 - 0
src/helloLine/helloLine2.java

@@ -0,0 +1,94 @@
+package helloLine;
+
+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 helloLine2 extends HttpServlet {
+
+	/**
+	 * Constructor of the object.
+	 */
+	public helloLine2() {
+		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 {
+//        lineDAO linedao=new lineDAO();
+//        ArrayList<lineBean> array = (ArrayList<lineBean>) linedao.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();
+        
+        lineDAO2 linedao2=new lineDAO2();
+        ArrayList<lineBean> array2 = (ArrayList<lineBean>) linedao2.listAll();
+        System.out.println("s"+array2);
+        response.setContentType("text/html; charset=utf-8");
+        JSONArray json2=JSONArray.fromObject(array2);
+        System.out.println(json2.toString());
+        PrintWriter out2 = response.getWriter();  
+        out2.println(json2);  
+        out2.flush();  
+        out2.close();
+	}
+
+	/**
+	 * Initialization of the servlet. <br>
+	 *
+	 * @throws ServletException if an error occurs
+	 */
+	public void init() throws ServletException {
+	}
+
+}

+ 71 - 0
src/helloLine/lineBean.java

@@ -0,0 +1,71 @@
+package helloLine;
+
+public class lineBean {
+	 private String name;
+	 private int year;
+	 private float score;
+	 private String name1;
+	 private int year1;
+	 private float score1;
+	 private String name2;
+	 private int year2;
+	 private float score2;
+	    public String getName() {
+	        return name;
+	    }
+	    public void setName(String name) {
+	        this.name = name;
+	    }
+	    public int getYear() {
+	    	return year;
+	    }
+	    public void setYear(int year) {
+	    	this.year = year;
+	    }
+	    public float getScore() {
+	        return score;
+	    }
+	    public void setScore(float score) {
+	        this.score = score;
+	    }
+	    
+	    
+	    public String getName1() {
+	    	return name1;
+	    }
+	    public void setName1(String name1) {
+	    	this.name1 = name1;
+	    }
+	    public int getYear1() {
+	    	return year1;
+	    }
+	    public void setYear1(int year1) {
+	    	this.year1 = year1;
+	    }
+	    public float getScore1() {
+	    	return score1;
+	    }
+	    public void setScore1(float score1) {
+	    	this.score1 = score1;
+	    }
+	    
+	    
+	    public String getName2() {
+	    	return name2;
+	    }
+	    public void setName2(String name2) {
+	    	this.name2 = name2;
+	    }
+	    public int getYear2() {
+	    	return year2;
+	    }
+	    public void setYear2(int year2) {
+	    	this.year2 = year2;
+	    }
+	    public float getScore2() {
+	    	return score2;
+	    }
+	    public void setScore2(float score2) {
+	    	this.score2 = score2;
+	    }
+}

+ 81 - 0
src/helloLine/lineDAO.java

@@ -0,0 +1,81 @@
+package helloLine;
+
+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 lineDAO {
+	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<lineBean> listAll() {
+		ArrayList<lineBean> list = new ArrayList<lineBean>();
+		PreparedStatement pstmt = null;
+//		PreparedStatement ps1 = null;
+		//PreparedStatement ps2 = null;
+		ResultSet rs = null;
+		//ResultSet rs1 = null;
+		//ResultSet rs2 = null;
+		try {
+			pstmt = this.getConnection().prepareStatement("SELECT * FROM line where name = '詹皇'");
+//			ps1 = this.getConnection().prepareStatement("SELECT * FROM line where name = '詹皇'");
+//			ps2 = this.getConnection().prepareStatement("SELECT * FROM line where name = '戴维斯'");
+			rs = pstmt.executeQuery();
+//			rs1 = ps1.executeQuery();
+//			rs2 = ps2.executeQuery();
+			
+			while (rs.next()) {
+				lineBean line = new lineBean();
+				line.setYear(rs.getInt("year"));
+				line.setScore(rs.getFloat("score"));
+				
+				list.add(line);
+				System.out.println("连接数据库成功");
+			}
+//			while (rs1.next()) {
+//				lineBean li1 = new lineBean();
+//				li1.setScore1(rs1.getFloat("score1"));
+//				list.add(li1);
+//			}
+//			while (rs2.next()) {
+//				lineBean li2 = new lineBean();
+//				li2.setName2(rs1.getString("name2"));
+//				li2.setScore2(rs2.getFloat("score2"));
+//				list.add(li2);
+//				System.out.println("连接数据库成功:"+li2);
+//			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				connection.close();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		return list;
+	}
+
+
+
+
+}

+ 81 - 0
src/helloLine/lineDAO2.java

@@ -0,0 +1,81 @@
+package helloLine;
+
+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 lineDAO2 {
+	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<lineBean> listAll() {
+		ArrayList<lineBean> list = new ArrayList<lineBean>();
+		PreparedStatement pstmt = null;
+		PreparedStatement ps1 = null;
+		PreparedStatement ps2 = null;
+		ResultSet rs = null;
+		//ResultSet rs1 = null;
+		ResultSet rs2 = null;
+		try {
+			pstmt = this.getConnection().prepareStatement("SELECT * FROM line where name = '詹皇'");
+			//ps1 = this.getConnection().prepareStatement("SELECT * FROM line where name = '詹皇'");
+			ps2 = this.getConnection().prepareStatement("SELECT * FROM line where name = '戴维斯'");
+			rs = pstmt.executeQuery();
+			//rs1 = ps1.executeQuery();
+			rs2 = ps2.executeQuery();
+			
+//			while (rs.next()) {
+//				lineBean line = new lineBean();
+//				line.setYear(rs.getInt("year"));
+//				line.setScore(rs.getFloat("score"));
+//				
+//				list.add(line);
+//				System.out.println("连接数据库成功");
+//			}
+//			while (rs1.next()) {
+//				lineBean li1 = new lineBean();
+//				li1.setScore1(rs1.getFloat("score1"));
+//				list.add(li1);
+//			}
+			while (rs2.next()) {
+				lineBean li2 = new lineBean();
+				li2.setName2(rs2.getString("name"));
+				li2.setScore2(rs2.getFloat("score"));
+				list.add(li2);
+//				System.out.println("连接数据库成功:"+li2);
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				connection.close();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		return list;
+	}
+
+
+
+
+}