没有连接到MSSQL服务器2008年Android应用 [英] android application not connecting to mssql server 2008

查看:141
本文介绍了没有连接到MSSQL服务器2008年Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的layout.xml

this is my 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>

这是我的activitymain.java

this is my activitymain.java

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

}

我没有得到这code任何异常或任何错误上按计划在模拟器的帮助运行此我请米如果在数据库表中的数据manipukating替代,那么请指引我,我试图通过JSON访问太多,但话又说回来,我没有得到任何数据了。

I am not getting any exception or any error in this code on running this in emulator help me pls m on a schedule. If there is an alternative for manipukating data in the database table then please guide me i have tried to access through json too but then again I was not getting any data back.

推荐答案

不要从你的应用程序直接连接到数据库(它就像拿着黑客的欢迎板)。使用Web服务。

Don't connect directly to database from your app (its like holding a welcome board for hackers). Use a webservice.

这篇关于没有连接到MSSQL服务器2008年Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆