[Android] Connect to MySQL (unfinished)

JDBC download

You can't connect to the DB directly from Android, so you need a JDBC driver. If you didn't install MySQL, you can download and install it from this link called Connector / J. https://dev.mysql.com/downloads/

There is mysql-connector-java-8.0.18.jar in the hierarchy of C: \ Program Files (x86) \ MySQL \ Connector J 8.0, so add it to the build path.

Build path setting

Add the jar file under app / libs. In addition, add the following to build.gradle.

dependencies {
    compile files('libs/mysql-connector-java-8.0.18.jar)
}

After that, the sync button will probably appear, so sync.

・ Reference URL https://qiita.com/icchi_h/items/8ce738ce8511ef69c799

permission

Permission is required because it uses the network. Added the following.

<manifest>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Connection program to MySQL

MainActivity.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
 //データベースに接続
            Connection con = MySqlConnect.getConnection();
 //ステートメントオブジェクトを作成
            Statement stmt = (Statement) con.createStatement();

            //SQL
            String mySql = "select date from table;";
            ResultSet rs = stmt.executeQuery(mySql);

            while(rs.next()) {
                Toast.makeText(getApplicationContext(), rs.getString("date"), Toast.LENGTH_LONG).show();
            }

 //オブジェクトを解放
            rs.close();
            stmt.close();
            con.close();

        } catch (Exception e) {
        }

MySqlConnect.java

class MySqlConnect{
  static Connection getConnection() throws Exception {
 //JDBCドライバのロード
    Class.forName("com.mysql.jdbc.Driver");
 //各設定
 String url = "jdbc: mysql: // hostname / dbname";
    String user = "user_name";
    String pass = "password";
 //データベースに接続
    Connection con = DriverManager.getConnection(url,user,pass);
    return con;
  }
}

・ Reference URL https://qiita.com/ks_513/items/0b286c4932a8e36c672e

Where in trouble

Currently, the following error has occurred and it is incomplete. ・ ** ClassLoader referenced unknown path ** -** java.lang.UnsupportedOperationException **

I tried setting the build path, reviewing the gradle file, etc., but I could not break through.

Impressions

I thought it would be easy, but it takes a lot of time. I've seen various sites, but I don't think it's relevant because there wasn't much written about asynchronous processing. But https://qiita.com/ks_513/items/0b286c4932a8e36c672e There is a statement at the bottom of that that asynchronous processing is better, so it seems worth trying.

Recommended Posts

[Android] Connect to MySQL (unfinished)
[Java] Connect to MySQL
Connect to MySQL 8 with Java
Connect to Aurora (MySQL) from a Java application
Introduction to Android Layout
How to install MySQL
To connect from Spring to MySQL on virtual server (unsolved)
I started MySQL 5.7 with docker-compose and tried to connect
Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2) error
I want to connect to Heroku MySQL from a client
Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2) error remedy
Introduction to Android application development
Connect to multiple MySQL instances with SSL enabled in JDBC
Connect from Java to PostgreSQL
Connect to DB with Java
Can't connect to local MySQL server through socket'/var/lib/mysql/mysql.sock' (2) until resolution.
Try to introduce OpenCV to Android application
Operation to connect multiple Streams @Java
Rails new fails to install mysql
Dynamically switch the database to connect to
Android Development-Try to display a dialog-
Cannot connect Javaplayfreamwork and Mysql (unresolved)
How to "hollow" View on Android
mysql2 fails to install with bundle install
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
How to connect Heroku and Sequel
Easy to use Cloud Firestore (Android)
Change DB from SQLite to MySQL
Android memo-I was addicted to Permission
Connect to Rails server with iPhone
Rails DB PostgreSQL changed to MySQL
[Android] Convert Android Java code to Kotlin
Update MySQL from 5.7 to 8.0 with Docker
[Android] How to make Dialog Fragment
docker + mysql can't connect / Can't connect to local MySQL server through socket'var / run / mysqld / mysqld.sock' (2)
[Docker] Does not connect to MySQL workbench ... DB container port forwarding settings