Java代码在NetBeans中正常工作,但在android 6.0

时间:2016-10-10 09:40:30

标签: java android sql

我的名字是Phuc。我遇到的问题是我的Java代码在NetBeans中正确运行,但是当我成功构建后复制并粘贴到Android Studio时。我跑进了我的手机(6.0)并且没有任何效果。我不知道为什么。请帮我。 (我的目的是从Android 6.0 App中的SQL Server发送和接收值)

这是我的Java代码:

<package javaapplication1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Phuc
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */

    private final Connection conn;



        public JavaApplication1() throws Exception
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://asdasdasa.mssql.somee.com;Database=asdasdasa;UserName=cip2017_SQLLogin_2;Password=oev2sm4ken";
            this.conn = DriverManager.getConnection(url);

        }

        public void thucthi() throws SQLException
        {
            Statement stm = this.conn.createStatement();
            String sql ="Update Butt set Value = 0 where Index1 = 2";
            stm.executeUpdate(sql);
        }

        public ResultSet Getdata(String tb) throws SQLException {
            ResultSet kq = null;
            Statement stm = this.conn.createStatement();
            String sql ="select Value from Varr";
            kq = stm.executeQuery(sql);
            return kq;
        }

        public void Close() throws SQLException {
            if(this.conn != null)
            {
                this.conn.close();
            }
        }

    public static void main(String[] args) throws Exception {
         JavaApplication1 ccc = new JavaApplication1();
            ResultSet rs = ccc.Getdata("Varr");
            ArrayList data = new ArrayList();
            while (rs.next())
            {
                data.add(rs.getString("Value"));
            }
            System.out.println(data.get(3));
            ccc.thucthi();
            ccc.Close();

    }
}

这是我的Android代码:

 package com.example.phuc.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {


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


        final TextView tv1 = (TextView) findViewById(R.id.textView);

        Button bt1 = (Button) findViewById(R.id.button);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ConnectToDB tt = new ConnectToDB();
                    tt.thucthi();
                    tt.Close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
}
    public static class ConnectToDB

    {
        private Connection conn;



        public ConnectToDB() throws Exception
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://asdasdasa.mssql.somee.com;Database=asdasdasa;UserName=cip2017_SQLLogin_2;Password=oev2sm4ken";
            this.conn = DriverManager.getConnection(url);
        }
        public void thucthi() throws SQLException{

            Statement stm = this.conn.createStatement();
            String sql ="Update Butt set Value = 1 where Index1 = 5";
            stm.executeUpdate(sql);
        }
        //public ResultSet Getdata(String tb) throws SQLException {
            //ResultSet kq = null;
           // Statement stm = this.conn.createStatement();
           // String sql ="select Value from Varr";
           // kq = stm.executeQuery(sql);
           // return kq;
        //}

        public void Close() throws SQLException {
            if(this.conn != null)
            {
                this.conn.close();
            }
        }
    }
}  

1 个答案:

答案 0 :(得分:0)

AsyncTask代码在这里:

    package com.example.phuc.myapplication;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {


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


        final TextView tv1 = (TextView) findViewById(R.id.textView);

        Button bt1 = (Button) findViewById(R.id.button);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       new bebi().execute();
                   }
               });
            }
        });

}

    public class bebi extends AsyncTask<String, Integer, String>
    {

        @Override
        protected String doInBackground(String... params) {

            ConnectToDB tt = null;
            try {
                tt = new ConnectToDB();
                tt.thucthi();
                tt.Close();

            } catch (Exception e) {
                e.printStackTrace();
            }


            return null;
        }
    }


    public static class ConnectToDB

    {
        private Connection conn;



        public ConnectToDB() throws Exception
        {

        }
        public void thucthi() throws SQLException {
            Statement stm = this.conn.createStatement();
            String sql ="select Value from Varr";
            stm.executeQuery(sql);
        }
        //public ResultSet Getdata(String tb) throws SQLException {
            //ResultSet kq = null;
           // Statement stm = this.conn.createStatement();
           // String sql ="select Value from Varr";
           // kq = stm.executeQuery(sql);
           // return kq;
        //}

        public void Close() throws SQLException {
            if(this.conn != null)
            {
                this.conn.close();
            }
        }
    }
}