2016-10-10 10 views
-4

私の名前はPhucです。私はJavaコードがNetBeansで正しく機能していたが、正常に構築された後にAndroid Studioにコピーアンドペーストしたときに問題になっています。私は私の電話(6.0)で走ったが、何も働かなかった。私は理由を知らなかった。私を助けてください。JavaコードはNetBeansで正しく動作しますが、Android 6.0では動作しません

<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

http://importblogkit.com/2015/07/does-not-work/ – Biffen

+0

Androidでは、メインスレッドがあります。短い操作とUIの更新に使用されます。ネットワークコールをしたい場合は、新しいスレッドを呼び出す必要があります。また、Android AsyncTaskを使用することもできます。 – toshkinl

+0

sqlserverのjdbc.jarを追加しましたか?また、私はAndroidアプリから直接リモートDBにアクセスしません。あなたのアプリケーションを逆コンパイルすると、自分でアクセスしたいすべての情報を見つけることができます。いくつかのWebサービスを使用していくつかの証券を追加します。 – AxelH

答えて

0

AsyncTaskこれは私のJavaコードである

(私の目的は、Android 6.0のアプリケーションでSQL Serverから値を送受信です)コードはここにあります:

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(); 
      } 
     } 
    } 
} 
関連する問題