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.
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 is required because it uses the network. Added the following.
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
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
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.
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