请问Android怎样连接远程MySQL数据库?Android客户端直接连接远程MySQL数据库mysql怎么和安卓连的方法如下:
String result = "";
//首先使用NameValuePair封装将要查询mysql怎么和安卓连的年数和关键字绑定
ArrayListNameValuePair nameValuePairs = new ArrayListNameValuePair();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//使用HttpPost封装整个SQL语句
//使用HttpClient发送HttpPost对象
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//将HttpEntity转化为String
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//将String通过JSONArray解析成最终结果
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;ijArray.length();i++){
JSONObject json_data = https://www.04ip.com/post/jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
}
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
虽然Android开发中可以直接连接数据库mysql怎么和安卓连,但是实际中却不建议这么做mysql怎么和安卓连,应该使用服务器端中转下完成 。
安卓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();
}
推荐阅读
- 4738g换什么cpu,4736zg升级cpu
- mysql怎么看密码 mysql密码怎么查
- 如何在小县城推广烧烤行业,小县城开烧烤店怎么样
- 违章拍摄是什么软件,违章拍摄是什么软件好用
- 电脑怎么看显卡,电脑怎么看显卡配置
- c语言如何定义带返回函数 c语言定义有返回值的函数
- flutter接入谷歌地图,flutter chromeos
- css3动画与js动画区别,css动画比js动画性能更好
- b站怎么单独给人直播,b站怎么自己直播