當前位置:首頁 » 安卓系統 » android連接mysql

android連接mysql

發布時間: 2022-01-08 02:47:23

Ⅰ Android手機app 鏈接伺服器的mysql 讀取資料庫

手機是不能直接去連接你伺服器的mysql資料庫

請在你的服務端寫代碼去連接mysql數據吧

Mysql連接方法

1. 載入資料庫驅動:Class.forName("org.gjt.mm.mysql.Driver"); //載入資料庫驅動

String url = "jdbc:mysql://localhost:3306/test";

String user = "root";

String passowrd = "123456";

2. 獲取資料庫連接Connection con數=DriverManager.getConnection(url,user,password)

3. 獲取SQL執行器 PreparedStatement prepare = con.prepareStatement("SQL語句")

4. 執行SQL語句,得到結果集 ResultSet result = prepare.executeQuery();

while(result.next()){

//讀取結果

}

最後不要忘記導入jdbc驅動包

純工手打字,請採納哈


Ⅱ android手機軟體開發中 怎麼連接Mysql資料庫

照你的說法應該是將手機作為客戶端,然後你的本機作為伺服器端,那你直接下個mysql的資料庫驅動,用jdbc連接不就行了,跟android本身沒太大關系。

Ⅲ android怎麼連接mysql資料庫

用Android程序去直連MySQL資料庫,覺得這樣做不好,出於安全等方面考慮。資料庫地址,用戶名密碼,查詢SQL什麼的都存在程序里,很容易被反編譯等方法看到。
建議把表示層和數據層邏輯分開,數據層對應網頁的表示層提供介面,同時在為Android手機端提供一個介面,簡介訪問資料庫,這介面可以2端都保持一致,比如XML+RPC或者json等等,Android端也有現成的東西能直接用,既安全又省事。

android 鏈接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=zzfeihua&password=12345&useUnicode=true&characterEncoding=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();

Ⅳ 請問Android怎樣連接遠程MySQL資料庫

沒直接連接過資料庫。我是連接的servlet然後給它查詢語句,servlet查詢後以xml形式返回數據。
安卓用HttpClient連接servlet,HttpClient使用詳情網路有。

Ⅳ 如何將Android應用程序連接到MySQL資料庫

1.首先需要安裝MySQL Server 5.1和navicat for mysql。這個安裝是很簡單的,網上很多教程,和安裝一般軟體差不多。只有在安裝MySQL Server 5.1時,要注意選擇字元編碼為gb2312(中文)那個選項。

Ⅵ android開發如何連接遠程MySql資料庫,初學者,最好給個具體的實例,灰常感謝

沒這么試過, 但是用android直接訪問資料庫恐怕有極大的安全隱患, 你的android讓人反編譯之後, 就能得到資料庫的鏈接字元串, 然後就能肆虐你的資料庫了,呵呵~

Ⅶ android 如何連接mysql資料庫,並且往資料庫裡面插入數據

去看看httpget和httppost,再看一下servlet就可以實現一個簡單的連接了,連接寫在servlet裡面就好

Ⅷ 安卓app 怎麼連接mysql

android 鏈接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=zzfeihua&password=12345&useUnicode=true&characterEncoding=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 proced by a broken compiler.)

Ⅸ 在Android平台如何上訪問mysql資料庫

直接使用普通的 java 資料庫連接方式即可。

Ⅹ android 連接mysql資料庫問題

我也是初學,給你參考一下
tomcat伺服器端:
public class LoginOrRegServlet extends HttpServlet { //登錄和注冊伺服器
private static final long serialVersionUID = 1L;
private static final String Driver = "com.mysql.jdbc.Driver"; //mysql驅動
private static final String ConnectUrl = "jdbc:MySQL://localhost:3306/itosystem";//連接資料庫的URL
private String User = "root"; //登錄資料庫的用戶名和密碼
private String Password = "num12369";
@Override
protected void service(HttpServletRequest request,HttpServletResponse response) { String connUserName; //取得請求中的用戶名和密碼
String connPassword;
String Action; //判斷請求中是登錄還是注冊
boolean isNull = false; //根據用戶名查詢資料庫是否查到
Connection conn = null;
DataOutputStream dos = null;
Statement statement = null;
ResultSet rs = null;
connUserName = request.getParameter("userName");//取得請求中的各種值
connPassword = request.getParameter("password");
Action = request.getParameter("action");
System.out.println(connUserName + "-" + connPassword+"-"+Action);
try { //設置驅動和連接資料庫
Class.forName(Driver);
conn = DriverManager.getConnection(ConnectUrl, User, Password);
} catch (Exception e1) {
e1.printStackTrace();
}
if(Action.equals("reg")){ //if請求為注冊
try {
if (conn != null) { //if資料庫連接成功

statement = conn.createStatement(); //取得對資料庫的操作對象
rs = statement.executeQuery("select password from user where name='"
+ connUserName + "'"); //根據請求中的用戶名使用mysql語句對資料庫的查詢操作
dos = new DataOutputStream(response.getOutputStream()); //對請求做出回應的對象
if(!rs.next()){ //如果沒有查到記錄,表明用戶名可以使用
statement.executeUpdate("insert into user values('"+connUserName+"','"+connPassword+"')");
//執行插入操作
dos.writeInt(5); //回應給客戶端的值(值可以隨意),5表示成功
}else{
dos.writeInt(10); //10表示用戶已存在
}
dos.flush(); //關閉各種對象
dos.close();
rs.close();
statement.close();
conn.close();
}else{
System.out.println("資料庫連接失敗");
}
} catch (Exception e) {
System.out.println("產生異常");
e.printStackTrace();
}
//注冊結束
}else if(Action.equals("login")){//if請求為登錄
try {
if (conn != null) {
statement = conn.createStatement();
rs = statement.executeQuery("select password from user where name='"+ connUserName + "'"); //查詢資料庫
dos = new DataOutputStream(response.getOutputStream());
while(rs.next()){//if查詢到有這個用戶名的記錄
String backPassword;
backPassword = rs.getString("password"); //從資料庫獲得這個用戶名的密碼
if (backPassword.equals(connPassword)) { //用資料庫返回的密碼和連接請求中的密碼對比
dos.writeInt(10); //如果相等,返回值,登錄成功
}else{
dos.writeInt(0); //否則登錄失敗
}
isNull = true; //查到有記錄,將其賦值為true
}
if(!isNull){ //if沒查到記錄
dos.writeInt(5); //返回5,表示用戶不存在
}
dos.flush(); //關閉各種對象
dos.close();
rs.close();
statement.close();
conn.close();
} else {
System.out.println("資料庫連接失敗");
}
} catch (Exception e) {
e.printStackTrace();

}
}
}//登錄操作結束
}

熱點內容
上傳文件文件夾找不到 發布:2024-09-20 00:26:32 瀏覽:914
承台箍筋加密區 發布:2024-09-20 00:26:31 瀏覽:227
筆記本什麼配置能流暢運行cf 發布:2024-09-20 00:14:19 瀏覽:951
實測華為編譯器 發布:2024-09-19 23:50:52 瀏覽:821
linux匯總 發布:2024-09-19 23:46:39 瀏覽:452
阿里雲伺服器環境搭建教程 發布:2024-09-19 23:21:58 瀏覽:837
黃色文件夾圖標 發布:2024-09-19 23:19:22 瀏覽:684
mysql資料庫導出導入 發布:2024-09-19 23:00:47 瀏覽:183
lua腳本精靈 發布:2024-09-19 23:00:41 瀏覽:659
任務欄文件夾圖標 發布:2024-09-19 22:54:25 瀏覽:101