I want to get data from table in MySQL and to show it in TextView, but I have some problems when I try to connect with database.
I'm using Eclipse for Android, and when I try to get data from MySQL in Java Project it works, but when I use Android Project it doesn't work.
Does anyone know how can I connect MySQL with Android Project using MySQL JDBC driver?
Or to give me other advice how to connect Android Project with MySQL?
If you want to connect to Mysql database from Android you just need to follow these steps:
Use the next code for connecting:
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e){
System.err.println("Cannot create connection");
}
try{
connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname","root","password");
Statement statement = connection.createStatement();
String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
query = query +"'" +variable+"'";
ResultSet result = statement.executeQuery(query);
}catch(Exception e){
System.err.println("Error");
}
Advice: If the instance of the drivers doesn't give any errors but you get an exception with the connection you should try to remove the Target SDK version from your manifest, as for some versions this gives problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With