Mysql connection

yawmbox

Member
Licensed User
Longtime User
hi all.
i've googled many hours before decide to ask your help, but i didn't find any solution for my trouble. :BangHead: :BangHead: :BangHead:

i've to create the android-client version of a server/client application. this application store all data into a mysql database with a lot of tables/records and a discrete traffic from server and more clients.
i've tried the json solution for connecting the android clients to the database but it's very slow for that use.

anyone know any other way to connect an android clients to the mysql database directly or in a very fast mode?
 

yawmbox

Member
Licensed User
Longtime User
yes, i've to download about 3500 records in a single query when the apps is created and on my archos 7 home tablet this take almost 2 minutes.
compared to this the other query are very smaller/faster.
 
Upvote 0

yawmbox

Member
Licensed User
Longtime User
in the last 2-3 days i've search a solution to connect my android app to a mysql server directly and i found the right way to do this in java.

this is what i did:
- create new android project on eclipse
- import in my referenced library the jar file linked below (mysql-connector-java.jar) that is part of libmysql-java linux packet
- add the instruction "import java.sql.*" to use the library mentioned above

i've created a database with one table with 2 fields, id and data.
in the example below i've opened a connection to mysql server and execute a query "select * from table_name".
all works fine.

mr. erel, can you implement a mysql library with this?

B4X:
package com.android.mysql;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import java.sql.*;

public class AndMysqlActivity extends Activity {
    /** Called when the activity is first created. */
   private static final String url = "jdbc:mysql://mysql-server/db_name";
    private static final String user = "username";
    private static final String pass = "password";
    private Integer c = 0;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* setContentView(R.layout.main); */
        
        dbconn();
    }
    
    public void dbconn() {
       TextView tv1 = new TextView(this);
       Button b1 = new Button(this);
       String testo;
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
       
       try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection(url, user, pass);
          /* System.out.println("Database connection success"); */
          tv1.setText("Database connection success");
          ll.addView(tv1);
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("select * from table_name");
          while(rs.next()) {
             testo = "Id: " + rs.getInt(1);
             tv1 = new TextView(this);
             tv1.setText(testo);
             ll.addView(tv1);
             testo = "Data: " + rs.getString(2);
             tv1 = new TextView(this);
             tv1.setText(testo);
             ll.addView(tv1);
             /*System.out.println("Id " + rs.getInt(1));
             System.out.println("Data " + rs.getString(2));
             System.out.println("");*/
          }
       }
       catch(Exception e) {
          /* e.printStackTrace(); */
          tv1 = new TextView(this);
          tv1.setText(e.toString());
          ll.addView(tv1);
       }
       
       b1.setText("Reload " + c++);
       b1.setGravity(Gravity.CENTER);
       b1.setClickable(true);
       b1.setOnClickListener(new OnClickListener() { 
          public void onClick(View viewParam) {
             dbconn();
          }
       });
       
       ll.addView(b1);
       
       this.setContentView(ll);
    }
}

mysql-connector-java.jar - link
 

Attachments

  • screenshot.jpg
    screenshot.jpg
    41 KB · Views: 486
Upvote 0

yawmbox

Member
Licensed User
Longtime User
Hi Erel,
I think that such functionality can increase dramatically the interest of the Android developers community toward B4A... I'll try, even if I'm not completely convinced that it is an easy task. My request isn't a shortcut, but starts from the concept that anyway, for you, that task can be mostly semplified, not starting from scratch, but starting from the work done for the existing db library. Isn't correct ? Thanks.
 
Upvote 0

yawmbox

Member
Licensed User
Longtime User
can you try the code that i've posted before?
this code work well. i'm not a java developer, i've studied 3 days before writing this code and to make it work. and for writing a full mysql library in java how long should i work? :confused:
i don't search a shortcut, i don't have the skills for do that.
otherwise, please, can you send me your sql library java code so i can adapt this to mysql? :sign0085:
if i can create a mysql library than i send my work here so you can include this library in b4a.
 
Last edited:
Upvote 0

yawmbox

Member
Licensed User
Longtime User
can you help me to create the first object of the library?

if i set my java class @Shortname("MysqlHandler"), how can i do this:

B4X:
Dim mh As MysqlHandler
Dim c As Cursor

mh.Initialize(host, dbname, user, pass)
c = mh.query("select * from table_name")
 
Upvote 0

yawmbox

Member
Licensed User
Longtime User
when i'm ready to export jar/xml of my java library, how can i export the referenced library into my output jar? :sign0085:

thanks a lot.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I'm not sure what the real question is but ....

I assume that you are using Eclipse and have generated an xml file with BADoclet and have exported the jar file. Both xml and jar need to be placed in your additional libraries folder and the library should appear in the Libs tab when you restart the IDE or right-click and Refresh on the Libs tab. After that reference it like any other library.

I have the xml generated directly into my additional libraries folder and export my jar there as well so after a recompilation all I need do is refresh the Libs tabs to get the new version loaded in the IDE.
 
Upvote 0

yawmbox

Member
Licensed User
Longtime User
yes, i've created a lib in eclipse/java project.
in this java project i've added an external jar to the build path.

when i create the jar file this external jar aren't copied into my final jar and then i've an error when in b4a i call the classes contained into the external jar.
 
Upvote 0

yawmbox

Member
Licensed User
Longtime User
not working.
i've copied the jar+xml external lib that i used to create my java library in the additional libraries folder, selected in the tab lib in b4a and compiled it successful (even before of this inclusion), but now when the application start on android device give me an interrupt error.
 
Upvote 0

yawmbox

Member
Licensed User
Longtime User
ok, now i've an application that works great but with a touch-up.

i've created the mylibrary.jar/xml that have in the build path an external third party jar. this external resource is not included in my jar file because eclipse don't do that.

after i've created the b4a project that use my library, i've to import in a new eclipse android project the object folder that b4a create into the b4a project folder. now i've to link again in the build path of eclipse project the external resources mylibrary.jar, thirdparty.jar, core.jar(b4a), b4ashared.jar(b4a) and delete from manifest file the installLocation directive because eclipse tell me that is an error.
now launch the application in the android emulator and all work great.

how can i include the thirdparty.jar into mylibrary.jar or into additional b4a libraries (i not have the xml file) to avoid this huge work?

:sign0085::sign0085::sign0085:
 
Upvote 0

yawmbox

Member
Licensed User
Longtime User
I've already told you in post #16 above. If it doesn't work you are doing something wrong because it works for AdMob, my IOIO and several other libraries.

i read your post but this don't works for me because i not have the xml file for the thirdparty.jar. i tried to build it in various mode but when i add the thirdparty library in b4a, this not works fine or else compiles and after the application at runtime gives me the error NoClassDefFound
 
Upvote 0
Top