Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android应用程序未连接到mssql server 2008_Java_Android_Sql Server - Fatal编程技术网

Java android应用程序未连接到mssql server 2008

Java android应用程序未连接到mssql server 2008,java,android,sql-server,Java,Android,Sql Server,这是我的layout.xml <LinearLayout 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:orientation="vertical" tools:c

这是我的layout.xml

<LinearLayout  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:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t1" />

   <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t2"
    />

    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t3"
    />

    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t4"
    />

    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t5"
    />

    <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn2"
    />

  </LinearLayout>
}


在emulator中运行此代码时,我没有收到任何异常或错误。请按计划帮助我。如果有其他方法可以管理数据库表中的数据,请指导我,我也尝试过通过json访问,但还是没有收到任何数据。

不要直接从应用程序连接到数据库(就像为黑客准备欢迎板一样)。使用Web服务。

尝试在android之外运行一些Web服务调用,以验证服务是否正确运行。此外,如果Web服务位于本地计算机上,我将验证是否没有任何防火墙/端口问题需要解决。

+1您不应该在应用程序中连接到数据库-这应该在服务器端完成。当我使用Web服务并在vs 2010中运行Web服务,然后在android中运行应用程序时,我仍然没有获得任何数据。我的android仿真器停止了。这是一个生产Web服务还是一个您已经验证的Web服务提供数据?
package com.example.mssqlconnect;

import android.os.Bundle;   
import java.sql.*;
import net.sourceforge.jtds.jdbc.*;
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.TextView;
import java.sql.*;
import net.sourceforge.jtds.jdbc.*;
public class MainActivity extends Activity {

public class A implements OnClickListener {

    @Override
    public void onClick(View arg0) {
        TextView tv2=(TextView)findViewById(R.id.t2);
        TextView tv3=(TextView)findViewById(R.id.t3);
        TextView tv4=(TextView)findViewById(R.id.t4);
        TextView tv5=(TextView)findViewById(R.id.t5);

        tv2.setText("Connected");
        Log.i("Android"," MySQL Connect Example.");
        Connection conn = null;
        try {
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        Class.forName(driver).newInstance();
        tv4.setText("2111");
        //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
        String connString = "jdbc:jtds:sqlserver://sony-VAIO/sony :1433/androiddata;encrypt=false;user=0cool;password=chevron;instance=SQLEXPRESS;";
        //String connString ="jdbc:jtds:sqlserver://(local);instance=MSSQLSERVER";
        String username = "0cool";
        String password = "chevron";
        tv4.setText("222222222");
        try{
        conn = DriverManager.getConnection(connString,username,password);
        //tv5.setText("2423424");



        Log.w("Connection","open");
        Statement stmt = conn.createStatement();
        ResultSet reset = stmt.executeQuery("select * from android_data");

           //Print the data to the console
           while(reset.next()){
            Log.w("Data:",reset.getString(1));
//                        Log.w("Data",reset.getString(2));
            //tv.setText(reset.getString(2));
            }
            conn.close();
            }
            catch(SQLException e)
             {
                tv5.setText(e.getMessage());
             }

        } catch (Exception e)
        {
                    //Log.w("Error connection","" + e.getMessage());
                    TextView tv1=(TextView)findViewById(R.id.t1);
                    tv1.setText(e.getMessage());
        }
        }


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn=(Button)findViewById(R.id.btn2);
    btn.setOnClickListener(new A());
}

@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;
}