LI99999XIA
lzxevol
2020-05-05 08:57
采纳率: 70%
浏览 451

Android studio3.5连接MySQL8.0出现下面问题,应该怎么解决。求帮忙

Android studio3.5连接MySQL8.0出现下面问题,应该怎么解决。
图片说明

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.regex.*;

public class DBHelper {

private static String diver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/student_attendance?characterEncoding=utf-8";
private static String user = "root";
private static String password = "lizzzzzzz";

public static Connection getConn(){
    Connection conn = null;
    try {
        Class.forName(diver);
        conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return conn;
}

}

public class MainActivity extends AppCompatActivity {

private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.bt_send);
    textView = (TextView) findViewById(R.id.tv_response);
}
@Override
protected void onStart() {
    super.onStart();
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //连接数据库进行操作需要在主线程操作
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Connection conn = null;
                    conn =(Connection) DBHelper.getConn();
                    Log.v("MainActivity","连接成功");
                    String sql = "select * from student";
                    Statement st;
                    try {
                        st = (Statement) conn.createStatement();
                        ResultSet rs = st.executeQuery(sql);
                        while (rs.next()){
                            Message msg = new Message();
                            msg.what =TEST_USER_SELECT;
                            handler.sendMessage(msg);
                        }
                        st.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    });
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐