How Can Android Apps Access Mysql?
Solution 1:
In android their is helper class which has parent class Sqlite which has all the data members and functions to access the through this class.Through this class you can read,write and open data.To know more about this read this link
http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android
To connect to a database you need a Connection object. The Connection object uses a DriverManager. The DriverManager passes in your database username, your password, and the location of the database.
Add these three import statements to the top of your code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
To set up a connection to a database, the code is this:
Connectioncon= DriverManager.getConnection( host, username, password );
See this example
try (
// Step 1: Allocate a database "Connection" object
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:8888/ebookshop", "myuser", "xxxx"); // MySQL// Connection conn = DriverManager.getConnection(// "jdbc:odbc:ebookshopODBC"); // Access// Step 2: Allocate a "Statement" object in the Connection
Statement stmt = conn.createStatement();
) {
// Step 3: Execute a SQL SELECT query, the query result// is returned in a "ResultSet" object.
String strSelect = "select title, price, qty from books";
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
System.out.println();
ResultSet rset = stmt.executeQuery(strSelect);
// Step 4: Process the ResultSet by scrolling the cursor forward via next().// For each row, retrieve the contents of the cells with getXxx(columnName).
System.out.println("The records selected are:");
int rowCount = 0;
while(rset.next()) { // Move the cursor to the next row
String title = rset.getString("title");
double price = rset.getDouble("price");
int qty = rset.getInt("qty");
System.out.println(title + ", " + price + ", " + qty);
++rowCount;
}
System.out.println("Total number of records = " + rowCount);
} catch(SQLException ex) {
ex.printStackTrace();
}
// Step 5: Close the resources - Done automatically by try-with-resources
}
Solution 2:
The most spread method to connect to a remote MySQL database from an Android device, is to put some kind of service into the middle. Since MySQL is usually used together with PHP, the easiest and most obvious way to write a PHP script
to manage the database and run this script using HTTP protocol from the Android system.
You can refer: Connect to MySQL using PHP on android as a start.
Additional note: Java traditionally uses JDBC connections to manage data sources. There are many available frameworks that can manage this more efficiently. These frameworks make it easier to write data-access codes and are easier to manage than traditional JDBC code. Such frameworks are available for Android too. Search for them. I'm sure you will find some answers. :)
Solution 3:
Use php on server side to connect and maintain MySql Database, then you can use some service to execute that php scripts from Android.If you want to know how to connect PHP MySql And Android here is an example http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ this uses wamp/lamp server.
if you want to create a database within the app, you can use SqLite Database. Which is very useful when your app requires to maintain an internal Database.Here is an example that illustrate the use of Sqlite http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/
Post a Comment for "How Can Android Apps Access Mysql?"