Connect to MySQL with Android using JDBC

I used the following code to connect MySQL to localhost from Android. It only displays the actions specified in the catch section. I do not know if this is a connection problem or not.

 package com.test1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Test1Activity extends Activity { /** Called when the activity is first created. */ String str="new"; static ResultSet rs; static PreparedStatement st; static Connection con; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView tv=(TextView)findViewById(R.id.user); try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://10.0.2.2:8080/example","root",""); st=con.prepareStatement("select * from country where id=1"); rs=st.executeQuery(); while(rs.next()) { str=rs.getString(2); } tv.setText(str); setContentView(tv); } catch(Exception e) { tv.setText(str); } } } 

When this code executes, it displays "new" in avd.

 java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom 

Can anyone suggest some solution? And thanks in advance

+8
android mysql jdbc
source share
7 answers

You cannot access the MySQL MySQL database initially. EDIT: You can actually use JDBC, but not recommended (or it may not work?) ... see Android JDBC not working: ClassNotFoundException for driver

Cm

http://www.helloandroid.com/tutorials/connecting-mysql-database

http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/8339-connect-android-mysql-database-tutorial.html

Android cannot connect directly to the database server. Therefore, we need to create a simple web service that will transfer queries to the databases and return a response.

http://codeoncloud.blogspot.com/2012/03/android-mysql-client.html

For most [good] users, this may be good. But imagine that you get a hacker who grabs your program. I decompiled my own applications and its scary what I saw. What if they get your username / password in your database and cause damage? Bad

+11
source share

This code works constantly !!! created by diko (Turkey)

 public void mysql() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } thrd1 = new Thread(new Runnable() { public void run() { while (!Thread.interrupted()) { try { Thread.sleep(100); } catch (InterruptedException e1) { } if (con == null) { try { con = DriverManager.getConnection("jdbc:mysql://192.168.1.45:3306/deneme", "ali", "12345"); } catch (SQLException e) { e.printStackTrace(); con = null; } if ((thrd2 != null) && (!thrd2.isAlive())) thrd2.start(); } } } }); if ((thrd1 != null) && (!thrd1.isAlive())) thrd1.start(); thrd2 = new Thread(new Runnable() { public void run() { while (!Thread.interrupted()) { if (con != null) { try { // con = DriverManager.getConnection("jdbc:mysql://192.168.1.45:3306/deneme", "ali", "12345"); Statement st = con.createStatement(); String ali = "'fff'"; st.execute("INSERT INTO deneme (name) VALUES(" + ali + ")"); // ResultSet rs = st.executeQuery("select * from deneme"); // ResultSetMetaData rsmd = rs.getMetaData(); // String result = new String(); // while (rs.next()) { // result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n"; // result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n"; // } } catch (SQLException e) { e.printStackTrace(); con = null; } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } else { try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); } 
+4
source share

If you need to connect your application to the server, you can do this through PHP / MySQL and JSON http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ . The Mysql connection code must be in the AsynTask class. Do not run it in the main topic.

+2
source share

Do you want to save your database on mobile devices? Use sqlite instead of mysql.

If the idea is to store the database on the server and access it from mobile devices. Use the web service to retrieve / modify data.

0
source share

Another approach is to use a virtual JDBC driver that uses a three-tier architecture: your JDBC code is sent via HTTP to a remote servlet that filters the JDBC code (configuration and security) before passing it to the JDBC MySql driver, the result is sent to you via HTTP. There are several free programs that use this technique. Just google "android jdbc driver over http".

0
source share
 public void testDB() { TextView tv = (TextView) this.findViewById(R.id.tv_data); try { Class.forName("com.mysql.jdbc.Driver"); // perfect // localhost /* * Connection con = DriverManager .getConnection( * "jdbc:mysql://192.168.1.5:3306/databasename?user=root&password=123" * ); */ // online testing Connection con = DriverManager .getConnection("jdbc:mysql://173.5.128.104:3306/vokyak_heyou?user=viowryk_hiweser&password=123"); String result = "Database connection success\n"; Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from tablename "); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { result += rsmd.getColumnName(1) + ": " + rs.getString(1) + "\n"; } tv.setText(result); } catch (Exception e) { e.printStackTrace(); tv.setText(e.toString()); } } 
0
source share

try changing in the gradle file targetSdkVersion to 8

 targetSdkVersion 8 
0
source share

All Articles