Android 开发 。。。如何连接到服务器上的mysql数据库1、首先输入代码
public void SendByHttpClient(final String id, final String pw){
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("应用部署服务器上的IP地址:/HttpClientDemo/Login");//服务器地址,指向Servlet
ListNameValuePair params=new ArrayListNameValuePair();//将id和pw装入list
params.add(new BasicNameValuePair("ID",id));
params.add(new BasicNameValuePair("PW",pw));
final UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"utf-8");//以UTF-8格式发送
httpPost.setEntity(entity);
HttpResponse httpResponse= httpclient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200)//在200毫秒之内接收到返回值
2、然后再输入下方的代码:
{
HttpEntity entity=httpResponse.getEntity();
String response=EntityUtils.toString(entity1, "utf-8");//以UTF-8格式解析
Message message=new Message();
message.what=USER_LOGIN;
message.obj=response;
handler.sendMessage(message);使用Message传递消息给线程
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
3、最终,测试结果图 , 如下:
安卓app 怎么连接mysqlandroid 链接mysql数据库实例:
package com.hl;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AndroidMsql extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sqlCon();
}
});
}
private void mSetText(String str){
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText(str);
}
private void sqlCon(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
try {
String url ="jdbc:mysql://192.168.142.128:3306/mysql?user=zzfeihuapassword=12345useUnicode=truecharacterEncoding=UTF-8";//链接数据库语句
Connection conn= (Connection) DriverManager.getConnection(url); //链接数据库
Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from user";//查询user表语句
ResultSet rs=stmt.executeQuery(sql);//执行查询
StringBuilder str=new StringBuilder();
while(rs.next()){
str.append(rs.getString(1)+"\n");
}
mSetText(str.toString());
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
不过eclipse老是提示:
warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
如何在Android Studio上利用云数据库Mysql实现在线登录注册?首先得会android开发和service后台开发 。给你个思路:
服务端部署好连接数据库写登录注册接口app端画登录注册页面调用对应的接口获得登录注册信息
请问Android怎样连接远程MySQL数据库?Android客户端直接连接远程MySQL数据库的方法如下:
String result = "";
推荐阅读
- 特技飞行员游戏攻略手游,特技飞行员多少个g
- qq订阅号防沉迷怎么添加公众号,订阅号怎么添加2020
- 手机pdf文件怎么修改,手机上修改pdf文件
- 搞直播叫别人做什么工作,开直播的人怎么称呼
- 计算函数c语言程序 c语言计算函数公式的格式
- chatgpt人物设定,chatbot原型
- 安卓国产解谜游戏有哪些,安卓解谜类游戏排行榜
- 即时战略类游戏摄影机需求,即时战略类型游戏
- java能调用go语言吗 java可以用goto语句吗