Androidstudio连接MySQL5.6数据库
程序员文章站
2022-06-21 19:15:53
使用环境:Androidstudio4.0 MySQL5.6 mysql-connector-java-5.1.30-bin.jarmainactivity.javapackage com.example.testmysql56;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;im...
使用环境:Androidstudio4.0 MySQL5.6 mysql-connector-java-5.1.30-bin.jar
补充一下,数据库用户需要赋予权限
GRANT ALL PRIVILEGES ON 数据库名.表名 TO '用户'@'地址' IDENTIFIED BY '连接时验证的密码' WITH GRANT OPTION;
数据库名.表名:是给用户该表的所有权限(*.*表示所有数据库和表)
地址:是允许访问的IP地址,%代表允许所有
mainactivity.java
package com.example.testmysql56;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private Button bt_ConMysql;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_ConMysql = findViewById(R.id.bt_ConMysql);
bt_ConMysql.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Connection_Init());
}
}).start();
}
});
}
public Connection Connection_Init()
{
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
long in1 = System.currentTimeMillis();
try {
connection = DriverManager.getConnection(
"jdbc:mysql://电脑的IPv4地址:3306/数据库名?useUnicode=true&characterEncoding=utf8",
"root",
"密码");// 输入连接参数
} catch (SQLException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("连接耗时" + (end - in1));//连接耗时
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/bt_ConMysql"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="连接"
/>
</LinearLayout>
注意清单文件中需要添加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testmysql56">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
将jar包复制到libs中
按照如下步骤,将jar包导入依赖
虚拟机运行
点击连接
查看日志,连接成功了
本文地址:https://blog.csdn.net/ITarmi/article/details/107122803