2012-07-13 56 views
1

我想創建一個可以運行SQL查詢到我的SQL Server數據庫的android應用程序。
這裏是我的基本思想是:是否有可能執行一些數據庫查詢,用戶可以從android應用程序寫入查詢?

  1. 我創建1個文本框和1個按鈕的android應用。
  2. 用戶可以在文本框中編寫他們想要的查詢(例如INSERT INTO .., UPDATE ..,DELETE ..或EXEC [存儲過程])。
  3. 然後用戶按下按鈕執行該查詢,以在我的SQL服務器數據庫的SQL 中運行。

我到目前爲止已經取得了哪些是使用的PhoneGap + jQuery Mobile的,我可以從我的WCF服務使用JSON一些數據連接到我的SQL Server的Android應用程序,但現在我想運行一些查詢我從android應用程序寫入並將其運行到我的數據庫。可能嗎?你能告訴我推薦教程嗎?謝謝:)

+0

而不是要求教程,你應該問一個具體的問題,可以給出答案。展示你的工作,展示你的研究,然後詳細解釋什麼阻止你做某事。 – nil 2012-07-13 06:35:53

+0

@nil好的,謝謝你的建議:)對不起 – blankon91 2012-07-13 06:40:15

回答

1

它可以做到。

  1. 創建一個帶有的EditText一個Android應用程序和按鈕

  2. 用戶可以鍵入要運行的查詢(例如INSERT INTO ..,UPDATE ..,DELETE ..或EXEC [存儲過程])到EditText

你可以像下面這樣做,使它更加用戶友好。

  1. 爲用戶提供一些通過按鈕插入/更新/刪除的選項。

  2. 添加一個微調與所有表名稱。

  3. 如果用戶選擇一個表名,則顯示一些_EditText_s用於獲取用戶所需的所有列。

此外,您可以根據需要使用不同的方法進行相應的調用。它會更簡單,更方便用戶使用。

我不知道它是否有意義。

+0

首先感謝你的回答,它打開我的腦海開始吧。以及用戶想寫自定義查詢和自由寫它,所以我不能讓你的答案與你所說的按鈕。而我還沒有完成,但現在我想開始製作它。那麼我會更新或創建我的問題,如果我卡住了。謝謝 :) – blankon91 2012-07-13 06:44:58

0

亞斯您可以親愛的..

我只是從SQL Server選擇數據的成功,我相信很快我正在挑釁達到插入更新和刪除了。

現在你也可以按照這個教程,它實際上是在不同的語言,但你可以通過android和eclipse正在使用英語,所以只需按照我的步驟。

首先製作一個項目並從此鏈接下載jtdc驅動程序,但請確保jtdc驅動程序版本爲「1.2」。7"

http://sourceforge.net/projects/jtds/files/jtds/1.2.7/
把這個jar文件在libs文件夾,寫點擊libs文件夾,並建立加jar文件,並採取從libs文件夾,然後按確定這是SQL Server的參考圖書館永遠不要忘記這一點。

現在只要按照我的代碼波紋管。

做一個xml文件 「sqlservermain.xml」

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".SqlServerMain" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="22dp" 
     android:text="SQL SERVER" /> 

    <Button 
     android:id="@+id/btn_enter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/edt_name" 
     android:layout_alignBottom="@+id/edt_name" 
     android:layout_alignParentRight="true" 
     android:text="Enter" /> 

    <ListView 
     android:id="@+id/lst_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/btn_enter" 
     android:layout_below="@+id/btn_enter" 
     android:layout_marginTop="19dp" > 
    </ListView> 

    <EditText 
     android:id="@+id/edt_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/lst_name" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="15dp" 
     android:ems="10" /> 

</RelativeLayout> 

然後讓彼此XML文件,該文件是 「sql.xml」 文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="#000000" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#00BFFF" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView1" 
     android:layout_toRightOf="@+id/imageView1" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

現在創建一個java文件,它是「sqlservermain.java」文件。

package com.sqlserver; 

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 java.util.Map; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 


public class SqlServerMain extends Activity { 
     private Button _btnenter; 
    private EditText _edtval; 
    private ListView _lstname; 
    private Connection connect; 
    private SimpleAdapter AD; 

    private void declaration() { 

     _btnenter = (Button) findViewById(R.id.btn_enter); 
     _edtval = (EditText) findViewById(R.id.edt_name); 
     _lstname = (ListView) findViewById(R.id.lst_name); 

    } 

    private void Initialization() { 
     declaration(); 
     _edtval.setText("Select Value"); 
     connect = CONN("user","pass","db","server"); 
    } 

    @SuppressLint("NewApi") 
    private Connection CONN(String _user, String _pass, String _DB, 
      String _server) { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     Connection conn = null; 
     String connURL = null; 
     try { 
      Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
      connURL = "jdbc:jtds:sqlserver://" + _server + ";" 
        + "databaseName=" + _DB + ";user=" + _user + ";password=" 
        + _pass + ";"; 
      conn = DriverManager.getConnection(connURL); 
     } catch (SQLException se) { 
      Log.d("Error", se.getMessage()); 
      // Toast.makeText(this, "Error : " + se,Toast.LENGTH_LONG).show(); 
     } catch (ClassNotFoundException ce) { 
      Log.d("Error", ce.getMessage()); 
      // Toast.makeText(this, "Error : " + ce,Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      Log.d("Error", e.getMessage()); 
      // Toast.makeText(this, "Error : " + e, Toast.LENGTH_LONG).show(); 
     } 

     return conn; 
    } 

    public void QuerySQL(String COMANDOSQL) { 
     ResultSet rs; 
     try { 
      Statement statement = connect.createStatement(); 
      rs = statement.executeQuery(COMANDOSQL); 

      List<Map<String, String>> data = null; 
      data = new ArrayList<Map<String, String>>(); 

      while (rs.next()) { 
       Map<String, String> datanum = new HashMap<String, String>(); 
       datanum.put("A", rs.getString("Table Field 1")); 
       datanum.put("B", rs.getString("Table Field 2")); 
       data.add(datanum); 
      } 
      String[] from = { "A", "B" }; 
      int[] views = { R.id.textView2, R.id.textView1 }; 
      AD = new SimpleAdapter(this, data, R.layout.sql, from, views); 
      _lstname.setAdapter(AD); 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sqlservermain); 
     Initialization(); 
     _btnenter.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       QuerySQL(_edtval.getText().toString()); 
      } 
     }); 
    } 

} 

而現在是時候給的 「AndroidManifest.xml中」

這是

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 

anjoy這就是它retriving一些權限。 的數據。

相關問題