Android通过Servlet连接阿里云服务器的mysql (servlet编程)

  • 最近想做一些跟服务器相关的Android手机端开发,所以入手了半年的阿里云服务器,给服务器选择Linux的系统刚好也学习下Linux的相关的知识。
  • 废话不多说,通过Eclipse的IDE进行服务器端的开发。实现代码是根据LFF_Coder改写的。
    • 新建Dynamic Web Project

    • 由于要连接Mysql,所以创建Libs文件夹, 添加mysql-connector-java-5.1.29-bin.jar,然后Add to build path。
    • 在Java Resources文件夹下新建class(例子:hello.class)
  • import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.google.gson.Gson;
    
    public class hello extends javax.servlet.http.HttpServlet {
    
    	private static final long serialVersionUID = 1L;
    
    	static String driver = "com.mysql.jdbc.Driver";
    	static String url = "jdbc:mysql://云服务器ip:3306/mydb";
    	static String user = "用户";
    	static String password = "密码";
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse res)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		// resp.getWriter().write("hello,tomcat");
    
    		res.setContentType("application/json;charset=utf-8");
    		req.setCharacterEncoding("utf-8");
    
    		PrintWriter out = res.getWriter();
    		 		
    		HashMap resultList = new HashMap<>();
    		try {
    			resultList = getData();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			out.println("erro,sorry");
    			
    		}
    		//将返回的数据(HashMap通过Gson以Json的方式返回客户端)
    		  Gson gson = new Gson();
    		  String result = gson.toJson(resultList);
    		  out.write(result);
    		  out.close();
    
    	}
    
    	@Override
    	protected void doPost(HttpServletRequest req, HttpServletResponse res)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		if (null == req) {
    			return;
    		}
    		res.setContentType("application/json;charset=utf-8");
    		req.setCharacterEncoding("utf-8");
    		res.setCharacterEncoding("utf-8");
    
    		PrintWriter out = res.getWriter();
    		String username = req.getParameter("user_name");
    		String password = req.getParameter("password");
    		if (username.equals("admin")) {
    			if (password.equals("123")) {
    				out.println("0"); // /< 正确
    			} else {
    				out.println("2"); // /< 密码错误
    			}
    		} else {
    			out.println("1"); // /< 用户名错误
    		}
    		out.flush();
    		out.close();
    	}
    
    	// 到Mysql获得数据
    	private HashMap getData() throws Exception {
    
    		try {
    			Class.forName(driver);
    			System.out.println("成功链接");
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    
    		Connection conn = null;
    		try {
    			conn = (Connection) DriverManager
    					.getConnection(url, user, password);
    			System.out.println("成功connection");
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    		//从userinfo 表中遍历出所有记录,然后将username 与 passwd字段保存在Hashmap中
    		Statement statement = (Statement) conn.createStatement();
    		String sql = null;
    		sql = "select * from userinfo";
    		ResultSet rs = statement.executeQuery(sql);
    		List resultList = new ArrayList();
    		HashMap hash = new HashMap<>();
    		
    		while (rs.next()) {
    			resultList.add(rs.getString("username"));
    			hash.put(rs.getString("username"), rs.getString("passwd"));
    		}
    
    		
    		return hash;
    	}
    
    }
    

  • 将servlet程序导出,右键-->export-->War file。
  • 将War包上传至云服务的Tomcat的Webapps文件夹下,记住,工程所依赖的包都得上传到Tomcat的Lib文件夹下(因为这个问题折腾了半天!)
  • 重启Tomcat服务即可。

你可能感兴趣的:(Android通过Servlet连接阿里云服务器的mysql (servlet编程))