在第一个我想知道“是否可以将Android应用程序连接到SQL Server?”
如果是,那么我面临一些问题。 我添加了jtds-1.2.5.jar
这是我的 MainActivity:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText e1;
Button b1;
TextView t1;
String str = null;
String conUrl = null;
String uname,pass;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText)findViewById(R.id.editText1);
b1 = (Button)findViewById(R.id.button1);
t1 = (TextView)findViewById(R.id.textView1);
str = "SELECT [Name] from user_mast where username = 'a'";
abc();
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//querySQL(str);
Toast.makeText(getBaseContext(), "In Click", Toast.LENGTH_SHORT).show();
}
});
}
void abc()
{
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
uname = "sa";
pass = "123";
///////// This is the problem statement It is not get executed..
Connection connect = DriverManager.getConnection("jdbc:jtds:sqlserver://server:1433/AlphaHotel;user="+ uname +";password=" + pass);
Statement statement=connect.createStatement();
ResultSet rs=statement.executeQuery(str);
while(rs.next()){
t1.setText(rs.getString(0));
}
connect.close();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
以下是错误Logcat:(错误可能仅限前两行)
12-31 06:40:33.884: W/IInputConnectionWrapper(2376): showStatusIcon on inactive InputConnection
12-31 06:40:34.973: W/System.err(2376): android.os.NetworkOnMainThreadException
12-31 06:40:34.973: W/System.err(2376): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-31 06:40:34.973: W/System.err(2376): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-31 06:40:34.983: W/System.err(2376): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-31 06:40:34.983: W/System.err(2376): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-31 06:40:35.033: W/System.err(2376): at java.net.Socket.tryAllAddresses(Socket.java:108)
12-31 06:40:35.033: W/System.err(2376): at java.net.Socket.<init>(Socket.java:177)
12-31 06:40:35.033: W/System.err(2376): at java.net.Socket.<init>(Socket.java:149)
12-31 06:40:35.033: W/System.err(2376): at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:259)
12-31 06:40:35.033: W/System.err(2376): at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:311)
12-31 06:40:35.063: W/System.err(2376): at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:187)
12-31 06:40:35.063: W/System.err(2376): at java.sql.DriverManager.getConnection(DriverManager.java:175)
12-31 06:40:35.063: W/System.err(2376): at java.sql.DriverManager.getConnection(DriverManager.java:140)
12-31 06:40:35.063: W/System.err(2376): at com.example.z.MainActivity.abc(MainActivity.java:61)
12-31 06:40:35.094: W/System.err(2376): at com.example.z.MainActivity.onCreate(MainActivity.java:38)
12-31 06:40:35.094: W/System.err(2376): at android.app.Activity.performCreate(Activity.java:5104)
12-31 06:40:35.094: W/System.err(2376): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-31 06:40:35.094: W/System.err(2376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-31 06:40:35.094: W/System.err(2376): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-31 06:40:35.094: W/System.err(2376): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-31 06:40:35.103: W/System.err(2376): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-31 06:40:35.103: W/System.err(2376): at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 06:40:35.103: W/System.err(2376): at android.os.Looper.loop(Looper.java:137)
12-31 06:40:35.103: W/System.err(2376): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-31 06:40:35.113: W/System.err(2376): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 06:40:35.113: W/System.err(2376): at java.lang.reflect.Method.invoke(Method.java:511)
12-31 06:40:35.113: W/System.err(2376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 06:40:35.153: W/System.err(2376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 06:40:35.153: W/System.err(2376): at dalvik.system.NativeStart.main(Native Method)
12-31 06:40:35.323: I/Choreographer(2376): Skipped 35 frames! The application may be doing too much work on its main thread.`
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.z"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.z.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
请查看所有代码并告诉我
我应该添加什么。
可能是什么问题。
谢谢。
答案 0 :(得分:3)
尝试将此添加到onCreate方法:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
不要忘记AndroidManifest文件中的互联网权限!
<uses-permission android:name="android.permission.INTERNET"/>
答案 1 :(得分:0)
我不确定Android与SQL Server的连接。但错误清楚地表明:
android.os.NetworkOnMainThreadException
这意味着您正尝试在主线程上创建网络连接,这在Android 4.0或更高版本中是不允许的。
尝试在后台线程或AsyncTask中创建网络连接。
阅读:http://developer.android.com/reference/android/os/AsyncTask.html