前记:Android真机访问PC端mysql等数据库时,一般的思路为:开通mysql远程连接,andriod直接通过pc的ip地址访问mysql,也就是android直连mysql。本人本想也通过此方法实现,无奈android开发平台是基于Android Studio的,其不可导入mysqk的jdbc.jar包,出现如Android Studio已有sql资源包,不可再导入此类jar包的错误。查找了半天也没找出解决的办法。而且此方法虽符合逻辑,但是存在不少漏洞,不安全。
最后我选择了一种基于servlet为服务器的中转实现方式,其主要的思路为:通过servlet搭建的服务器将mysql中需要的数据取出来发布在以tomcat为支持的网页上,android端直接访问该网页,将网页中的数据全部取出。当然也可以通过webservice实现中转。。。本文主要讲解服务器代码,客户端详解请看本人下一篇博文!!!
话不多说,直接上代码:(源代码地址:http://download.csdn.net/detail/snadijssajskkj/9904869)
1、服务器端主要webtest.java代码(在eclipse中新建Dynamic Web Project工程,工程名为webtt)
<span style="font-size:14px;">package com.tt;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.tt.Deviceinformation;
public class webtest extends HttpServlet {
private String idAndstatement,sqlqurey,sqlupdate,deviceid,statement;
private Connection conn;
private Statement stm=null;
private ResultSet result=null;
private PrintWriter outPrintWriter;
private PreparedStatement pstm=null;
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public webtest(){
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//sex="男";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/自己的数据库名称?" +
"user=root&password=123456&useUnicode=true&characterEncoding=utf-8");
stm=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
sqlqurey="select * from deviceif ";//查询表语句
result = stm.executeQuery(sqlqurey);
if (result!=null) {//下面用到servlet
response.setContentType("text/html;charset=UTF-8");//这句必须放在下一句之前
outPrintWriter =response.getWriter();
//<span style="font-family: Arial, Helvetica, sans-serif;">idAndstatement为android返回给服务器的字段,里面包含的信息,自己自定义</span>
idAndstatement=new String(request.getParameter("idAndstatement").getBytes("ISO-8859-1"),"UTF-8");
//对<span style="font-family: Arial, Helvetica, sans-serif;">idAndstatement进行拆解(以“,”为分界)</span>
String [] splitStrings=idAndstatement.split("\\,");
for(int i=0;i<splitStrings.length;i++){
//System.out.println(splitStrings.length);
}
deviceid=splitStrings[0];
statement=splitStrings[1];
//更新数据
if(!deviceid.equals("t")){
sqlupdate="update deviceif set 仪表状态=? where 设备号=?";
pstm=(PreparedStatement) conn.prepareStatement(sqlupdate);
//改变数据库记录性别
pstm.setString(1, statement);
pstm.setString(2, deviceid);
pstm.executeUpdate();
//outPrintWriter.print("性别:"+sex);
}
while(result.next()){//从mysql选取数据
//获得客户端发来的参数信息
<span style="font-family: Arial, Helvetica, sans-serif;">/*outPrintWriter.write("用户名:"+name+"的账户密码为:"+result.getString(1)+""+result.getString(2));*/</span>
//<span style="font-family: Arial, Helvetica, sans-serif;">outPrintWriter.write()用于向网页写数据</span>
outPrintWriter.write(result.getString(1)+" "+result.getString(2)+" "+result.getString(3)+" "+
result.getString(4)+" "+" "+result.getString(5)+" "+" "+result.getString(6)+" ");
outPrintWriter.println();
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
outPrintWriter.flush();
outPrintWriter.close();
//response.getWriter().write("link sucessfully!");
}
public void Json(){
}
//释放资源
public void destroy() {
super.destroy();
try{
if(result!=null){
result.close();//关闭数据库
result=null;
}
if(stm!=null){
stm.close();
stm=null;
}
if(pstm!=null){
pstm.close();
pstm=null;
}
if(conn!=null){
conn.close();//关闭后Statement,PreparedStatement也会关闭
conn=null;
}
}catch(Exception ex){
System.out.println("Error : " + ex.toString());
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}</span>
2、服务器端web.xml需要做响应修改,可直接替换本来web.xml(说明,HelloWorld任意取的,com.tt.webtest为在工程webtt下src下创建com.tt包,在包下新建webtest文件,如上)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.tt.webtest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
3、tomcat 安装并调试成功后,通过tomcat发布之后在浏览器输入http://localhost:8080/webtt/HelloWorld 即可等待android端的访问请求,从而自启动webtest中的doget()函数