前置工作 1. 安裝 TomCat 2. 下載及安裝 AppServ exe 內含 Appache 2.2/Php5/MySQL 3. 修改相關參數檔 Appache 2.2/conf/ httpd.conf, workers.properties TomCat/conf/server.xml 4. 在安裝目錄下之 www 目錄內新增一個 uploads 目錄
5. 將 login.php 及 GetRecord.php 置入 www 目錄內 login.php: 登錄用 GetRecord.php: 上傳及執行 SQL 用
Login to MySQL public 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";
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");
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()));
StringBuilder sb = new StringBuilder(); String line = null; // Read Server Response while((line = reader.readLine()) != null) { sb.append(line); break; } result=sb.toString(); res.setText("Login Successful");
Download mysql table lists txtSql.setText("show tables"); sql="show tables"; execSQL(sql); parseData(result); }catch(Exception e){ res.setText("Err "+e.getMessage()) ; blLog=false; }
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,passw ord);
res.setText("SQL Command successful"); txtResult.setText(result); } catch(Exception e) { Log.e("log_tag", e.getMessage().toString()); result=null; res.setText("Failed"); }
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();
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);
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"); }
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;
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 params = new ArrayList (); 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));
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"); }
inputStream.close(); result = builder.toString(); } catch(Exception e) { // Log.e("log_tag", e.toString()); } return result; }