Monday 8 July 2019

java - What's the trouble with Android Studio App connecting to MySQL using JDBC?



I understand that this is not the preferred way to do a connection from
Android to MySQL, but I know it is doable.



I've been working on this problem for Days,researching and applying new methods. I got too close to making a connection except I couldn't make a connection. I used different versions of JDBC connectors until the error disappeared, and still getting some unexplained errors. I want to see this though and make it work instead of giving up on it. I thought some may give me some suggestions instead of me looping in the same place.



As you see in the files below, all this simple program should do is adding information to MySql table when the button is clicked. IF the connection established I added a println statement to tell me that connection was established but that line does not print. :




UPDATE: Got this MySQL Exception :



com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Server connection failure during transaction. Due to underlying exception: 'java.net.SocketException: android.os.NetworkOnMainThreadException'. 


activity_main.xml :







xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.aero.myapplication.MainActivity">


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="New Button"
android:id="@+id/b"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />





MainActivity.java :






package com.example.aero.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {





super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button auth = (Button)findViewById(R.id.b);
auth.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){

String username="root";
String pass ="pass";

String conn_str="jdbc:mysql://192.168.0.10:3306/JAVA?autoReconnect=true&useSSL=false";

Connection conn=null;

try{
Class.forName("com.mysql.jdbc.Driver");
conn =(Connection) DriverManager.getConnection(conn_str,username,pass);

System.out.println("ESTABLISHED CONNEC");
Statement s = conn.createStatement();


String fname = "romeo";
String Lname="alpha";
String id ="meincode2";


String u = "USE JAVA;";
s.executeUpdate(u);
String insert = "INSERT INTO JAVA.users(Firstname,Lastname,ID) VALUES ('"+fname+"','"+Lname+"','"+id+"')";
s.executeUpdate(insert);

System.out.println("DONE !!!");
}catch (SQLException e){

} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
}
);






}
}






App:





apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"


defaultConfig {
applicationId "com.example.aero.myapplication"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {

minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'

//compile files('libs/mysql-connector-java-2.0.14-bin.jar')
//compile files('libs/mysql-connector-java-5.1.38-bin.jar')
compile files('libs/mysql-connector-java-5.0.8-bin.jar')
}





AndroidManifest.xml :







package="com.example.aero.myapplication">

android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

android:supportsRtl="true"
android:theme="@style/AppTheme">

















Answer



My Question was answered somewhere else with the help of @Scary Wombat.




Answer : Caused by: android.os.NetworkOnMainThreadException


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...