我想创建一个可以运行SQL Query到我的SQL Server数据库的android应用程序。
这是我的基本想法:
到目前为止,我使用的是一个使用phonegap + jQuery Mobile的Android应用程序,我可以通过连接到我的SQL Server的JSON从我的WCF服务获取一些数据,但现在我想运行一些我写的查询android应用程序并将其运行到我的数据库。可能吗?你能告诉我推荐教程吗?谢谢:))
答案 0 :(得分:1)
可以做到。
使用 EditText 和按钮创建一个Android应用程序。
用户可以在 EditText 中键入要运行的查询(例如INSERT INTO ..,UPDATE ..,DELETE ..或EXEC [store procedure])。
您可以像下面这样做,以使其更加用户友好。
为用户提供一些通过按钮插入/更新/删除的选项。
添加包含所有表名的 Spinner 。
如果用户选择了一个表名,请为从该用户获取值所需的所有列显示一些_EditText_s。
此外,您可以使用所需的输入,使用相应的方法进行相应的调用。它将更简单,更加用户友好。
我不知道这是否有意义。
答案 1 :(得分:0)
你可以亲爱的..
我只是从sql服务器中选择数据取得了成功,我很快就会确定,我也会很快达到插入更新和删除。
现在你也可以按照这个实际上使用不同语言的教程,但是你可以通过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"/>
很高兴认为它可以用于复原。
数据。