ITPub博客

首页 > 数据库 > > Android连接MySQL问题,驱动加载成功,连接却失败

Android连接MySQL问题,驱动加载成功,连接却失败

作者:doudourabbit 时间:2019-07-31 09:37:25 0 删除 编辑
<br>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MainActivity extends AppCompatActivity {
    private TextView textView,textTwo;
    private final static String URL = "jdbc:mysql://localhost:3306/librarydb";
    static String rootName = "root";
    static String PWD = "1997831";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.textView);
        textTwo= (TextView) findViewById(R.id.textTwo);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            textView.setText("驱动加载成功");
        } catch (ClassNotFoundException e) {
            textView.setText("找不到驱动程序类 ,加载驱动失败!");
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Connection conn= conn = (Connection) DriverManager.getConnection(URL,rootName,PWD);
                    textTwo.setText("数据库连接成功!");
                } catch (SQLException e) {
                    textTwo.setText("数据库连接失败!");
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4809/viewspace-3122701/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-25

  • 博文量
    193
  • 访问量
    1438