没有PHP的AndroidStudio中的mysql中的数据库连接

时间:2019-06-20 17:16:51

标签: java android mysql

实际上我想在没有php的情况下将mysql与android连接

  

我试图打印存储在mysql数据库中的数据。

     

数据库名称=“ android”
  端口号=“ 3006”
  sql =“ select * from tablename”;

private class Data1 extends AsyncTask<Void,Void,Void> {
                 @Override
                 protected Void doInBackground(Void... voids) {
                     Connection con= null;
                     String username="root";
                    String password="";
                     try {
                         Class.forName("com.mysql.cj.jdbc.Driver");
                        con= DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/android",username,password);
                         PreparedStatement stmt=con.prepareStatement("select * from login");
                         ResultSet rs=stmt.executeQuery();
                         while (rs.next()){
                             System.out.println(rs.getString(1));
                        }
                        con.close();
                     } catch (SQLException e) {
                         e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                         e.printStackTrace();
                     }
                     finally {
                         System.out.println("ok");
                    }

                     return null;
                 }

            }

>E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
>Process: com.example.login, PID: 10158
>java.lang.RuntimeException: An error occurred while executing doInBackground()

0 个答案:

没有答案