First time using an external database with android and i'm trying to set up the database connections. I created a basic table in the database just for testing purposes and am trying to connect and insert some data. This is my code:
public class DBInterface {
//TODO: Fix the database connections
private Connection conn = null;
private Statement statement = null;
public void connectToDatabase(){
Log.d("myTag", "This is my messagestart");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://mysql.**.***.***.***:3306", "DB_USER", "DB_PASSWORD");
statement = conn.createStatement();
statement.executeUpdate("INSERT INTO test (test) VALUES (15)");
Log.d("myTag", "This is my message");
} catch (SQLException ex) {
Log.d("myTag", "SQLException: " + ex.getMessage());
Log.d("myTag","SQLState: " + ex.getSQLState());
Log.d("myTag","VendorError: " + ex.getErrorCode());
}catch(Exception e){e.printStackTrace();}
}
and I call it in the onCreate of the MainActivity:
DBInterface db = new DBInterface();
db.connectToDatabase();
Log.d("myTag", "This is my message2");
No data is inserted but i also don't get any errors, i put the log messages in to try and see where it was getting to and the only output to the log is:
10-31 16:13:41.481 9918-9918/com.jacksteel.comp4 D/myTag: This is my messagestart
10-31 16:13:41.486 9918-9918/com.jacksteel.comp4 D/myTag: This is my message2
Not a direct answer, but you never want to connect your app directly to your database. Instead you should write some scripts on your server to perform INSERT, SELECT, UPDATE , DELETE.
Your app will then send requests to these scripts to execute the queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.