所以我創建了一個app
,現在我試圖將它連接到MySQL Server
。我目前從eclipse
到AVD
。將我的應用程序連接到在線MySQL服務器(工作臺)
所以我對連接到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
我已經添加了它在我的libs文件夾和Java構建路徑 – pap
與MySQL 3.0.17連接器試圖將所有的訪問到Asyn_Task裏面的'mysql' db – Sukan
你確定嗎?還有另一種方法可以做到嗎? 你也是'AsyncTask'嗎? – pap