将Android应用程序连接到远程mySQL数据库时出错

时间:2016-08-10 16:53:13

标签: java android mysql raspberry-pi3

我做了一个非常简单的应用程序只是为了稳定与我的SQL数据库在Raspberry Pi上的连接与JDBC,但当我尝试连接时,应用程序给我一个错误,任何建议?

enter image description here

代码:

public class MainActivity extends AppCompatActivity {

     private static final String url = "jdbc:mysql://192.168.1.65:3306/myDB";
     private static final String user = "user";
     private static final String pass = "userpass";

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     }

     public void connect(View v){
        TextView tv = (TextView) findViewById(R.id.textView);

        try{
             StrictMode.ThreadPolicy policy =
               new StrictMode.ThreadPolicy.Builder().permitAll().build();
             StrictMode.setThreadPolicy(policy);

             Class.forName("com.mysql.jdbc.Driver");
             Connection con = DriverManager.getConnection(url,user,pass);

             if(con!=null){
                 tv.setText("Connection Successfully");
             }else
                 tv.setText("FAAAAIL ");

         }catch (Exception e){
               e.printStackTrace();
               tv.setText(e.toString());
         }
     }
}

0 个答案:

没有答案