Problem: Difficulties connecting to MySQL from within an Android project using the JDBC driver, while the same code works in a Java project.
Solution:
To connect Android with MySQL effectively, follow these steps:
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"); }
Additional Advice:
The above is the detailed content of How to Connect Android to MySQL Using JDBC Driver?. For more information, please follow other related articles on the PHP Chinese website!