简体   繁体   English

将我的应用程序连接到在线MySQL服务器(工作台)

[英]Connect my app to online MySQL server (workbench)

So I have created an app and now I am trying to connect it to a MySQL Server . 因此,我创建了一个app ,现在尝试将其连接到MySQL Server I am currently working from eclipse through AVD . 我目前正在通过eclipse通过AVD

So my code for connecting to the SQL Server : 所以我的代码连接到SQL Server

try{

    Connection con = DriverManager.getConnection("jdbc:mysql://xxxx:3306/cl49-takeawa-cc7","cl49-takeawa-cc7","xxxx");
    PreparedStatement st = con.prepareStatement("select * from app_users where user_username='"+username2+"'");


    ResultSet rs = st.executeQuery();
    while(rs.next()){

        String username = rs.getString("user_username");

        if(username.equals(username2)){

            Toast.makeText(getBaseContext(), "This user already exist! Please try another name", Toast.LENGTH_SHORT).show();


        }

    }
    user_id++;

    PreparedStatement st1 = (PreparedStatement) con.prepareStatement("INSERT INTO user (user_id,user_realname,user_username,user_password,user_house_number,user_postcode,user_address,user_email) VALUES (?,?,?,?,?,?,?,?)");
    st1.setLong(1, user_id);
    st1.setString(2, fullname1);
    st1.setString(3, username2);
    st1.setString(4, password1);
    st1.setString(5, housenumber1);
    st1.setString(6, postcode2);
    st1.setString(7, address1);
    st1.setString(8, email1);
    st1.executeUpdate();
    st1.close();

    con.close();


}catch(Exception ex){

    System.out.println(ex);

}

And this is the error that I get: 这是我得到的error

java.sql.SQLException: No suitable driver

Any help? 有什么帮助吗?

Edit: 编辑:

        else{        
            new PostTask();
            Toast.makeText(getBaseContext(), "You have created an account! Time to eat!", Toast.LENGTH_SHORT).show();   
            Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
                startActivity(myIntent);
            }
        }
    });



}
// The definition of our task class
   private class PostTask extends AsyncTask<String, Integer, String> {
   @Override
   protected void onPreExecute() {
      super.onPreExecute();
   }

   @Override
   protected String doInBackground(String... params) {
      String url=params[0];
      try{

            Connection con = DriverManager.getConnection("jdbc:mysql://xxxx:3306/cl49-takeawa-cc7","cl49-takeawa-cc7","xxxx");
            PreparedStatement st = con.prepareStatement("select * from app_users where user_username='"+username2+"'");

            ResultSet rs = st.executeQuery();
            while(rs.next()){

                 String username = rs.getString("user_username");

                 if(username.equals(username2)){

                        Toast.makeText(getBaseContext(), "This user already exist! Please try another name", Toast.LENGTH_SHORT).show();


                 }

             }
            user_id++;

            PreparedStatement st1 = (PreparedStatement) con.prepareStatement("INSERT INTO user (user_id,user_realname,user_username,user_password,user_house_number,user_postcode,user_address,user_email) VALUES (?,?,?,?,?,?,?,?)");
            st1.setLong(1, user_id);
            st1.setString(2, fullname1);
            st1.setString(3, username2);
            st1.setString(4, password1);
            st1.setString(5, housenumber1);
            st1.setString(6, postcode2);
            st1.setString(7, address1);
            st1.setString(8, email1);
            st1.executeUpdate();
            st1.close();

            con.close();


        }catch(Exception ex){

            System.out.println(ex);

        }
    return url;
   }

   @Override
   protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
   }

   @Override
   protected void onPostExecute(String result) {
      super.onPostExecute(result);
   }
   }        

}

Now I don't get something back in the LogCat , but also I don't get any update on MySQL Server 现在我没有在LogCat得到任何东西,但是我也没有在MySQL Server上得到任何更新

添加mysql jdbc连接器并添加到您的项目中,它将解决该问题。

转到https://dev.mysql.com/downloads/connector/j/并下载jdbc驱动程序并添加到您的类路径中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM