17

前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

Embed Size (px)

Citation preview

Page 1: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml
Page 2: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

前置工作1. 安裝 TomCat2. 下載及安裝 AppServ2.5.10.exe

內含 Appache 2.2/Php5/MySQL3. 修改相關參數檔

Appache 2.2/conf/ httpd.conf, workers.properties

TomCat/conf/server.xml4. 在安裝目錄下之 www 目錄內新增一個 uploads

目錄

Page 3: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

5. 將 login.php 及 GetRecord.php 置入 www 目錄內login.php: 登錄用GetRecord.php: 上傳及執行 SQL 用

Page 4: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

Login to MySQLpublic void login(View view){ try{ db=txtDb.getText().toString(); username =

txtUser.getText().toString(); password =

txtPswd.getText().toString(); sql=txtSql.getText().toString(); String link=urlText.toString()

+"login.php";

Page 5: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

String data = URLEncoder.encode(“db”, “UTF-8”) + “=” + URLEncoder.encode(db, “UTF-8”); data += “&” + URLEncoder.encode(“username”, “UTF-8”) + “=” + URLEncoder.encode(username, “UTF-8”); data += “&” + URLEncoder.encode(“password”, “UTF-8”) + “=” + URLEncoder.encode(password, “UTF-8”); data += “&” + URLEncoder.encode(“query_string”, “UTF-

8”) + "=" + URLEncoder.encode(sql, "UTF-8");

Page 6: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

URL url = new URL(link);URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setConnectTimeout(30000);OutputStreamWriter wr = new

OutputStreamWriter (conn.getOutputStream()); wr.write( data ); wr.flush(); BufferedReader reader = new BufferedReader (new

InputStreamReader(conn.getInputStream()));

Page 7: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

StringBuilder sb = new StringBuilder(); String line = null;// Read Server Responsewhile((line = reader.readLine()) != null) { sb.append(line); break; } result=sb.toString(); res.setText("Login Successful");

Page 8: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

Download mysql table liststxtSql.setText("show tables"); sql="show tables"; execSQL(sql); parseData(result); }catch(Exception e){ res.setText("Err "+e.getMessage()) ; blLog=false; }}

Page 9: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

Sub Function: execSQL(sql)private void execSQL(String sql){ try{ strUrl=urlText.toString(); username = txtUser.getText().toString(); password = txtPswd.getText().toString(); txtResult.setText(""); sql=sql.toLowerCase(); db=txtDb.getText().toString(); result =

DBConnector.executeQuery(sql,strUrl,db,username,password);

Page 10: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

res.setText("SQL Command successful"); txtResult.setText(result); } catch(Exception e) { Log.e("log_tag", e.getMessage().toString()); result=null; res.setText("Failed"); } }

Page 11: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

Sub Function: parseData(result) private void parseData(String result){ try{ //res.setText(sql); ParseJsonData pjd=new

ParseJsonData(recType); int no=pjd.calComma(result)+1; FldNo=no; Fld=new String[no]; pjd.getFldName( result,Fld,FldNo); sql=txtSql.getText().toString(); sql=sql.toLowerCase();

Page 12: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

if(sql.startsWith("show create table")){ JSONArray jsonArray = new

JSONArray(result); JSONObject jsonData =

jsonArray.getJSONObject(0); txtResult.setText(jsonData.getString(Fld[1])); } if(sql.startsWith("select ")){ addFldSpin(spnFld); } int b=sql.compareTo("show tables"); if(b==0){ int rn=pjd.calRecord(result); tblAry =new String[rn]; recData=new String[rn][FldNo]; pjd.getRecord(result,Fld,recData);

Page 13: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

for(int ri=0;ri<rn;ri++){ tblAry[ri]=recData[ri][0]; } addTblSpin(spnTbl); } } catch(Exception e) { Log.e("log_tag",

e.getMessage().toString()); result=null; res.setText("Failed"); } }

Page 14: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

Class: DBConnector import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;

import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP;

Page 15: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

public class DBConnector { public static String executeQuery(String sql,String strUrl,String

db,String user,String pswd) { String result = ""; try { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new

HttpPost(strUrl+"GetRecord.php"); ArrayList<NameValuePair> params = new

ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("db", db)); //OK params.add(new BasicNameValuePair("username",

user)); //OK params.add(new BasicNameValuePair("password", pswd));

//OK params.add(new BasicNameValuePair("query_string",

sql)); httpPost.setEntity(new UrlEncodedFormEntity(params,

HTTP.UTF_8));

Page 16: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

HttpResponse httpResponse = httpClient.execute(httpPost);

//view_account.setText(httpResponse.getStatusLine().toString());

HttpEntity httpEntity = httpResponse.getEntity(); InputStream inputStream = httpEntity.getContent(); BufferedReader bufReader = new

BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);

StringBuilder builder = new StringBuilder(); String line = null; while((line = bufReader.readLine()) != null) { builder.append(line + "\n"); }

Page 17: 前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ2.5.10.exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml

inputStream.close(); result = builder.toString(); } catch(Exception e) { // Log.e("log_tag", e.toString()); } return result; }}