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

时间:2014-03-31 10:28:21

标签: java android mysql

所以我创建了一个app,现在我正在尝试将其连接到MySQL Server。我目前正在eclipseAVD工作。

所以我的代码连接到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);

}

这是我得到的error

java.sql.SQLException: No suitable driver

任何帮助?

修改

        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);
   }
   }        

}

现在我在LogCat中找不到任何内容,但我也没有获得MySQL Server

的最新消息

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

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