2011-01-26 12 views
3

私はMySQLサーバーに接続するAndroidアプリケーションを作成しています。今のところ、http://localhost:3306/を使用してXAMPP経由でコンピュータのMySQLサーバーをテストしています。以下のコードは厳密にJAVAアプリケーションとしてテストするときにうまく動作します。Android + MySQL(com.mysql.jdbc.Driverを使用)

import java.sql.*; 

public class MySQL{ 
    public static void main(String[] args) { 
    System.out.println("MySQL Connect Example."); 
    Connection conn = null; 
    String url = "jdbc:mysql://localhost:3306/"; 
    String dbName = "database"; 
    String driver = "com.mysql.jdbc.Driver"; 
    String userName = "root"; 
    String password = ""; 
    try { 
     Class.forName(driver).newInstance(); 
     conn = DriverManager.getConnection(url+dbName,userName,password); 
     System.out.println("Connected to the database"); 
     conn.close(); 
     System.out.println("Disconnected from database"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

ただし、Androidアプリケーションに追加すると、例外eエラーが発生します。

// interact with MySQL!!! 
private View.OnClickListener onSendRequest = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    EditText username = (EditText) findViewById(R.id.Huusername); 
     // String un = " " + username.getText().toString() + " "; 

     System.out.println("MySQL Connect Example."); 
     Connection conn = null; 
     String url = "jdbc:mysql://localhost:3306/"; 
     String dbName = "database"; 
     String driver = "com.mysql.jdbc.Driver"; 
     String userName = "root"; 
     String password = ""; 
     try { 
     Class.forName(driver).newInstance(); 
     conn = DriverManager.getConnection(url+dbName,userName,password); 
     Toast.makeText(getBaseContext(), 
     "Connected to the database.", Toast.LENGTH_LONG) 
     .show(); 
     conn.close(); 
     Toast.makeText(getBaseContext(), 
     "Disconnected form the database.", Toast.LENGTH_LONG) 
     .show(); 
     } catch (Exception e) { 
     Toast.makeText(getBaseContext(), 
     "Exception e.", Toast.LENGTH_LONG) 
     .show(); 
     e.printStackTrace(); 
     } 


    } 
}; 

私は私のAndroidアプリケーションをコンパイルしていますように、私は、コンソールで次のステートメントを注意してください:

[2011-01-26 16:05:54 - hookup]: Dxwarning: Ignoring InnerClasses attribute for an anonymous inner class 
(com.mysql.jdbc.interceptors.ResultSetScannerInterceptor$1) that doesn't come with an 
associated EnclosingMethod attribute. This class was probably produced by a 
compiler that did not target the modern .class file format. The recommended 
solution is to recompile the class from source, using an up-to-date compiler 
and without specifying any "-target" type options. The consequence of ignoring 
this warning is that reflective operations on this class will incorrectly 
indicate that it is *not* an inner class. 

私はこの文は私が取得しています例外とは何かを持っていると仮定します。誰かが私と正しい方向に私を導くことができるMySQLとAndroidで働いていますか? ありがとう

+0

ドライバを再コンパイルするには、おそらくソースコードがドライバに必要です。 –

答えて

0

あなたはContentProvidersを見てみましたか? http://developer.android.com/guide/topics/providers/content-providers.html

MySQLに直接アクセスするよりも、そのような実装(たとえばSQLiteデータベース)でデータにアクセスする方が多くなるでしょう。

+0

他の携帯電話の他のユーザーがデータを利用できるようにする必要があります。したがって、ContentProvidersは受け入れられる選択肢ではありません。 – Raddfood

-3

アンドロイドのリモートデータベースへの接続を提供するには、アンドロイドにリモートデータベースに接続するAPIがないため、WebServiceを使用する必要があります。

6

JDBCコネクタmysql-connector-java-3.0.17-ga-bin.jarを使用してください。これは動作する唯一のものです。

また、あなたのアプリケーションのアクティビティごとに接続のプールを実装する必要があります。ちょうどプライベートな "Connection" varを追加し、con = DriveManager.getConnection .....のようなOnCreateメソッドで初期化してくださいあなたはMYSQLにアクセスする必要があるたびにそのプライベートvarを使用します。

2

他のアプローチは、3層アーキテクチャを使用する仮想JDBCドライバを使用することです。JDBCコードは、HTTPを介してJDBCコード(構成&セキュリティ)をフィルタするリモートサーブレットに送信されてから、MySql JDBCドライバ。結果はHTTP経由で送り返されます。この手法を使用するフリーソフトウェアがいくつかあります。ちょうどGoogleの "HTTP経由のAndroid JDBCドライバ"です。

関連する問題