'Android Studio access MYSQL with JDBC
I've been trying to connect to MYSQL by using of JDBC for a while now but having no luck. I have my database set up already with some information inside and I've added the mysql-connector-java-5.1.35-bin library in the libs folder. The permission to access Internet and Network also is added in manifest. I run Android Studio in windows 7. After running it so far there is no syntax error but in my emulator, I get this message:
Test Error:java.sql.SQLException:Access Denied for user 'root'@'192.168.56.101'(using password:NO)
Which part of my work is wrong?! I've attached my code below.
public class MainActivity extends Activity {
//private static final String url = "jdbc:mysql://<server>:<port>/<database>";
public static final String LISTEN_ACTIVITY = "listenActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testDB();
}
public void testDB() {
TextView tv = (TextView)this.findViewById(R.id.txtv);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Log.i("Android", " MySQL Connection ok");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/k_sql1", "root", "");
// System.out.println("Database connection success");
Log.d("Android2","Line 2");
String result = ("");
Log.d("Android3", " Line 3");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from kpeople");
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
}
tv.setText(result);
} catch (Exception e) {
e.printStackTrace();
tv.setText("Test Error:"+e.toString());
Log.w("Android-system","system get connection");
}
}
}
Please help to solve the problem since there are many experts in here. Thank you guys.
Solution 1:[1]
It's not the client's fault. Your android device ip is 192.168.56.101. MySQL server root account can access by localhost by default. You should change the root access or create another account in MySQL.
Solution 2:[2]
In fact there was 2 problems, One from MYSQL permission in privileges that wasn't allow me to access to database, and the other problem was in coding. The existance of String result = (""); in my coding was meaningless and by removing it from code connection happened with no problem. The question that would rise in here is how to make my coding to have an auto update, mean by entrance of any new data into my database it automatically appear in my app too, any idea for auto update...
Solution 3:[3]
In your MySQL, create a user for the application, with scope '%', for e.g.
CREATE USER 'androidapp'@'%' IDENTIFIED BY 'password';
It means, that it can login from anywhere '%', if you use only 'localhost' it can o
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Leo Lin |
Solution 2 | Kazem Chm |
Solution 3 | nDCasT |