1

我想创建一个可以对我的 SQL Server 数据库运行 SQL 查询的 android 应用程序。
这是我的基本想法:

  1. 我创建了一个带有 1 个文本框和 1 个按钮的 android 应用程序。
  2. 用户可以在文本框中编写他们想要的查询(例如 INSERT INTO..、UPDATE..、DELETE.. 或 EXEC [存储过程])。
  3. 然后用户按下按钮执行该查询以在我的 SQL Server 数据库中运行。

到目前为止,我所做的是一个使用 phonegap + jQuery Mobile 的 Android 应用程序,我可以使用连接到我的 SQL Server 的 JSON 从我的 WCF 服务中获取一些数据,但现在我想运行一些我从 android 应用程序编写的查询和将它运行到我的数据库。可能吗?你能告诉我推荐教程吗?谢谢 :)

4

2 回答 2

1

可以办到。

  1. 使用EditTextButton创建一个 Android 应用程序。

  2. 用户可以在EditText中键入要运行的查询(例如 INSERT INTO..、UPDATE..、DELETE.. 或 EXEC [存储过程])。

您可以像下面那样执行此操作,以使其更加用户友好。

  1. 通过按钮为用户提供一些插入/更新/删除的选项。

  2. 添加一个包含所有表名的Spinner 。

  3. 如果用户选择一个表名,请为您需要从用户获取值的所有列显示一些 _EditText_s。

您也可以使用所需的输入制作单独的方法来相应地调用。它将不那么复杂,对用户更友好。

我不知道这是否有意义。

于 2012-07-13T06:35:55.507 回答
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 文件夹中获取并按 ok 即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"/>

享受这就是它的retriving。数据。

于 2013-08-23T11:21:16.163 回答