2014-02-12 76 views
0

我正在研究修改外部MySQL數據庫的android應用程序。我知道我可以使用中間的PHP/JSON服務來做到這一點,但我寧願使用JBDC,因爲連接速度更快,我的項目老師希望我這樣做。因爲這是我的第一個應用程序,我開始使用一個簡單的按鈕和一個操作(創建一個數據庫),它實際上工作(實際上是兩個按鈕,第一個不能在skd高於9的時候工作,AsyncTask不得不爲在他們使用):在android片段中的JDBC不工作?

package com.example.prova; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import java.sql.*; 

public class MainActivity extends Activity { 

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

     final Button button1 = (Button) findViewById(R.id.btconn1); 
     final Button button2 = (Button) findViewById(R.id.btconn2); 


     button1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       try 
       { 
        String URL = "jdbc:mysql://" + "192.168.1.200" + ":" + "3306"; 
        String USER = "app"; 
        String PASS = "android"; 

        Toast.makeText(getApplicationContext(), 
          "Conectando a servidor MySQL", 
          Toast.LENGTH_SHORT).show(); 

        Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        Connection conn = DriverManager.getConnection(URL, USER, PASS); 

        Toast.makeText(getApplicationContext(), 
          "Conectado Servidor MySQL", 
          Toast.LENGTH_LONG).show(); 

        Statement stmt = conn.createStatement(); 

        String SQL = "CREATE DATABASE SYNC"; 

        stmt.executeUpdate(SQL); 

        conn.close();    
       } 
       catch (ClassNotFoundException e) 
       { 
        Toast.makeText(getApplicationContext(), 
          "Error: " + e.getMessage(), 
          Toast.LENGTH_SHORT).show(); 
       } 
       catch (SQLException e) 
       { 
        Toast.makeText(getApplicationContext(), 
          "Error: " + e.getMessage(), 
          Toast.LENGTH_SHORT).show(); 
       } 
       catch (Exception e) 
       { 
        Toast.makeText(getApplicationContext(), 
          "Error: " + e.getMessage(), 
          Toast.LENGTH_LONG).show(); 
       } 

      } 
     }); 

     button2.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       new LED13ON().execute(); 
      } 
     }); 
     } 

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

    public class LED13ON extends AsyncTask<Void, Integer, Void> { 

     @Override 
     protected void onPostExecute(Void result){ 
      SystemClock.sleep(2000); 
     } 

     @Override 
     protected void onPreExecute(){ 
      SystemClock.sleep(2100); 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values){ 
      SystemClock.sleep(100); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0){ 

      try 
       { 
       String URL = "jdbc:mysql://" + "192.168.1.200" + ":" + "3306"; 
       String USER = "app"; 
       String PASS = "android"; 

       Class.forName("com.mysql.jdbc.Driver").newInstance(); 
       Connection conn = DriverManager.getConnection(URL, USER, PASS); 

       Statement stmt = conn.createStatement(); 

       String SQL = "CREATE DATABASE aSYNC"; 

       stmt.executeUpdate(SQL); 

       conn.close();    
       } 
      catch (ClassNotFoundException e) 
      { 
      } 
      catch (SQLException e) 
      { 
      } 
      catch (Exception e) 
      { 
      } 
      return null; 
       } 
    } 
} 

問題是,當我嘗試使用碎片,日食不返回錯誤回報,但JDBC代碼是行不通的。我知道那只是JDBC代碼不工作,因爲它進入LED13ON內部併產生SystemClock.sleep(2000),因爲該按鈕被標記了兩秒鐘。這是我對新一類片段的代碼:

package com.example.smarthome; 

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


import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.NavUtils; 
import android.support.v4.view.ViewPager; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 



public class Fragment_main extends Fragment { 
     public Fragment_main() { 
     } 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
       View rootView = inflater.inflate(R.layout.fragment_main,container, false); 

       Button btn = (Button) rootView.findViewById(R.id.btconn1); 
       btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick (View v) { 
        new LED13ON().execute(); 
       } 
       }); 

       return rootView; 
     } 

     public class LED13ON extends AsyncTask<Void, Integer, Void> { 

      @Override 
      protected void onPostExecute(Void result){ 
       SystemClock.sleep(2000); 
      } 

      @Override 
      protected void onPreExecute(){ 
       SystemClock.sleep(2100); 
      } 

      @Override 
      protected void onProgressUpdate(Integer... values){ 
       SystemClock.sleep(100); 
      } 

      @Override 
      protected Void doInBackground(Void... arg0){ 

       try 
        { 
        String URL = "jdbc:mysql://" + "192.168.1.200" + ":" + "3306"; 
        String USER = "app"; 
        String PASS = "android"; 

        Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        Connection conn = DriverManager.getConnection(URL, USER, PASS); 

        Statement stmt = conn.createStatement(); 

        String SQL = "CREATE DATABASE aSYNC"; 

        stmt.executeUpdate(SQL); 

        conn.close();    
        } 
       catch (ClassNotFoundException e) 
       { 
       } 
       catch (SQLException e) 
       { 
       } 
       catch (Exception e) 
       { 
       } 
       return null; 
      } 
     } 

} 

所以我不明白爲什麼被相同的代碼它不是第二個應用程序的工作,已經改變了setOnClickListener的片段工作。誰能幫我?我真的很喜歡用我的應用程序的滑動視圖,因爲我認爲它適合更多的Android Holo風格。

謝謝你的時間!

+0

我建議的第一件事就是記錄您發現的異常。其次,記錄不同部分的代碼,以確保它沒有到達這些部分。請注意,如果您啓動AsyncTask,則不會看到該按鈕掛起。這是一件好事。要記錄...'Log.w(「ClassName」,message);' – JRomero

+0

另外,爲什麼睡覺? – JRomero

回答

0

我記錄的例外,它給我的錯誤:現在我的應用程序工作

Class.forName ("com.mysql.jdbc.Driver").newInstance(); 

所以這一切,:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 

解決的辦法是添加的newInstance中的Class.forName正如我的意圖。謝謝你的一切!