工具:Android Studio,阿里云服务器ECS上部署的MySQL,mysql-connector-java-5.1.47.jar
问题描述:用putty远程连接云服务器的MySQL,可以操作其数据的增删查改,但是现在需要在手机端进行对数据库MySQL的操作,导入jar包并用jdbc方式连接,却始终没办法连接。
参考博文:https://blog.csdn.net/weixin_44462664/article/details/102572740
以下是小编通过查找资料,最终实现连接的过程,记录于此以便后续学习,也希望能对大家有所帮助。
过程:
1、导入jar包,从仓库下载mysql-connector-java-5.1.47.jar(地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java),复制到libs里面,右键mysql-connector-java-5.1.47.jar → Add as Library...
2、在AndroidManifest.xml里面添加一句,允许网络:
3、然后用jdbc方式连接。编写DBUtils.java,我直接放在了MainActivity.java同一目录下。
package connection;
import android.util.Log;
import java.sql.*;
public class DBUtils {
private static String driver = "com.mysql.jdbc.Driver";
private static Connection conn;
public static Connection getConn(){
try {
Class.forName(driver);
Log.d("数据库连接", "getConn: 数据库驱动加载成功");
String url = "jdbc:mysql://gongwnagip/数据库名字?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String user = "MySQL的用户名:默认为root";
String password = "MySQL的登录密码:默认没有";
conn = DriverManager.getConnection(url,user,password);
Log.d("数据库连接", "getConn: 数据库连接成功");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
Log.d("数据库连接", "getConn: 数据库连接失败");
return null;
}
return conn;
}
public static void closeConn(Connection conn, PreparedStatement preStmt, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preStmt!=null){
try {
preStmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
!!!注意!!!(连不上mysql的几个原因)
① String url中,数据库名字后面的 ?useUnicode=true&characterEncoding=utf-8&useSSL=false 十分重要,我之前一直连不上云服务器的mysql,这是原因之一。
② 在云服务器端配置安全组,开放3306端口;
③ 开启mysql远程登录,授权root远程登录,设置mysql的root的host为“%”:
用putty远程登录ECS,然后登录mysql;
mysql -u root -p
登录后,输入下面代码授权root远程登录:(123456为连接的密码)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges
授权后,刷新权限:
flush privileges
4、新建Item.java文件:
package connection;
public class Item {
private String account;
private String password;
private String date;
private String TEV;
private String speed;
public void setAccount(String account){
this.account =account;
}
public void setPassword(String password){
this.password =password;
}
public String getAccount(){
return account;
}
public String getPassword(){
return password;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTEV() {
return TEV;
}
public void setTEV(String TEV) {
this.TEV = TEV;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
}
新建item.xml:
5、新建Dao.java,用于操作数据库:
package connection;
import android.util.Log;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Dao {
private static Connection conn;
private static PreparedStatement preStmt;
private static ResultSet rs;
private String sql2 = "select * from wave ";
public List- waveList() throws SQLException {
List
- list = new ArrayList<>();
conn = DBUtils.getConn();
Log.d("数据库连接返回值", "login: "+conn);
preStmt = conn.prepareStatement(sql2);
rs = preStmt.executeQuery();
while(rs.next()){
Item item = new Item();
item.setDate(rs.getString(1));
item.setTEV(rs.getString(2));
item.setSpeed(rs.getString(3));
list.add(item);
}
DBUtils.closeConn(conn,preStmt,rs);
return list;
}
}
6、编写activity_main.xml文件,本例中只部署了几个按钮。
7、编写MainActivity.java:
package connection;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.robot.R;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button mbtn31;
private TextView mtv7;
private static final String TAG = "MainActivity活动";
private List- itemList = new ArrayList<>();
private ListView listView;
@SuppressLint("HandlerLeak")
private Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what){
case 1:
Log.d(TAG, "run: UI更新成功");
MyAdapter myAdapter= new MyAdapter(MainActivity.this, R.layout.item, itemList);
listView = (ListView)findViewById(R.id.list_item);
listView.setAdapter(myAdapter);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbtn31 = (Button) findViewById(R.id.btn_31);
mbtn31.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initLogin();
}
});
}
private void initLogin(){
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
}
private class MyThread implements Runnable{
@Override
public void run() {
Dao dao = new Dao();
try {
itemList = dao.waveList();
Log.d(TAG, "run: 数据库数据读取成功");
} catch (SQLException e) {
e.printStackTrace();
Log.d(TAG, "run: " +
"数据库数据读取失败");
}
Message msg = new Message();
msg.what =1;
myHandler.sendMessage(msg);
Log.d(TAG, "run: Message 唯一识别码 msg.what 发送成功");
}
}
}