安卓系統(tǒng)的遠程數(shù)據(jù)庫MySql操作
開發(fā)系統(tǒng):Android4.4.2 開發(fā)平臺:廣州微嵌安卓工業(yè)平板 開發(fā)工具:eclipse 開發(fā)語言:Java 使用的數(shù)據(jù)庫:MySql 在日常的開發(fā)安卓程序中,很多時候都涉及到大量的數(shù)據(jù)管理,雖然安卓系統(tǒng)有自帶的數(shù)據(jù)庫sqlite,但sqlite是輕量級的,有時候滿足不了使用的需求,這時候就需要連接遠程的數(shù)據(jù)庫進行對數(shù)據(jù)的管理,下面我們就來學(xué)習(xí)下在安卓上是如何連接遠程數(shù)據(jù)庫使用的,本次所使用的遠程數(shù)據(jù)庫是MySql,安裝在電腦上面,通過網(wǎng)絡(luò)連接MySql服務(wù)器,從而可以遠程操作數(shù)據(jù)庫。
在數(shù)據(jù)庫操作中使用的一個類Connection:連接表示從Java應(yīng)用程序到數(shù)據(jù)庫的鏈接 1、添加數(shù)據(jù)庫的驅(qū)動:在安卓上連接遠程數(shù)據(jù)庫需要先加載數(shù)據(jù)庫驅(qū)動,不同的數(shù)據(jù)庫的加載略有不同,這里以MySql為例,以下的所有操作都是針對MySql的.
try{
Class.forName("com.mysql.jdbc.Driver");
Toast.makeText(this, "數(shù)據(jù)庫加載成功", Toast.LENGTH_LONG).show();}catch(ClassNotFoundException e){
Toast.makeText(this, "數(shù)據(jù)庫加載失敗\n"+e, Toast.LENGTH_LONG).show();}添加mysql驅(qū)動說明:下載mysql的驅(qū)動文件”mysql-connector-java-5.0.8-bin.jar”,在項目根目錄下新建文件夾lib,將mysql的驅(qū)動文件”mysql-connector-java-5.0.8-bin.jar”拷貝到lib下
2、連接遠程數(shù)據(jù)庫,在連接前先確保遠程數(shù)據(jù)數(shù)據(jù)庫允許其它連接
try{
String ip = sqlipedit.getText().toString();
/*遠程服務(wù)器的ip跟端口號,使用賬號、密碼,不同的數(shù)據(jù)庫使用的連接端口、命令都不同
mysql使用的連接命令:jdbc:mysql//192.168.1.xxx:3306
*/
Connection con = DriverManager.getConnection("jdbc:mysql:"+"//192.168.1.64:3306", "admin","admin");
System.out.println("連接成功"); }catch(SQLException e){
e.printStackTrace();
System.out.println("失敗");}3、獲取數(shù)據(jù)庫服務(wù)器中已存在的所有數(shù)據(jù)庫
使用的命令:show databases
Statement statement = null;
ResultSet result = null;
try {
//顯示數(shù)據(jù)庫命令
String sql = "show databases";
statement = (Statement) conn.createStatement();
//執(zhí)行獲取數(shù)據(jù)庫的命令
result = statement.executeQuery(sql);
//獲取數(shù)據(jù)庫的數(shù)量
int size = result.getRow();
//獲取數(shù)據(jù)庫的名字
while(result.next()){
Log.d("DatabaseName", result.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (result != null) {
result.close();
result = null;
}
if (statement != null) {
statement.close();
statement = null;
}
} catch (SQLException sqle) {
}
}4、切換當(dāng)前連接所使用的數(shù)據(jù)庫:
try { if(!con.isClosed()){ //方法說明:設(shè)置此連接的目錄名稱,參數(shù)就是前面獲取到的數(shù)據(jù)庫的名字
con.setCatalog((String)databasename);
}
} catch (SQLException e) { // TODO 自動生成的 catch 塊
e.printStackTrace();
}5、執(zhí)行sql命令的方法有兩種: 一、Statement.excuteQuery(String sqlcmd):不對數(shù)據(jù)庫中的內(nèi)容做修改,只用于查詢 二、Statement.excuteUpdate()String sqlcmd)對數(shù)據(jù)庫中的內(nèi)容做修改,包括增刪改等,將該操作封裝成一個方法所有的操作可以直接使用
/*執(zhí)行mysql的修改命令,包括增、刪、改等命令
*參數(shù):conn:連接數(shù)據(jù)庫的鏈接,sql:數(shù)據(jù)庫的操作命令
*返回值:為成功操作的數(shù)據(jù)個數(shù),如:執(zhí)行修改數(shù)據(jù)的命令后,有n條列的數(shù)據(jù)被成功修改,返回值就是n.
*/public int query_l(Connection conn, String sql) {
int row = -1; if (conn == null) {
return -1;
}
Statement statement = null;
ResultSet result = null;
try {
statement = (Statement) conn.createStatement();
//該方法用于修改數(shù)據(jù)庫內(nèi)容的。
row = statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (result != null) {
result.close();
result = null;
}
if (statement != null) {
statement.close();
statement = null;
}
} catch (SQLException sqle) {
}
}
return row;
}6、獲取數(shù)據(jù)庫下的所有數(shù)據(jù)表
//使用命令:show tables from databasename//使用該命令可以獲取某一數(shù)據(jù)庫下的所有數(shù)據(jù)表String sql = "show tables from databasename";
Statement statement = null;
ResultSet result = null;try {
statement =(Statement)con.createStatement();
result = statement.executeQuery(sql);
while(result.next()){
Log.d("TableName",result.getString(1));
}
} catch (SQLException e) {
}7、獲取數(shù)據(jù)表下的所有字段跟數(shù)據(jù)
//獲取字段跟字段類型,使用命令:select from tableString sql = "select * from " + tablename;
String ss = "";
java.sql.PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
java.sql.ResultSetMetaData meta = rs.getMetaData();
//字段數(shù)量int columeCount = meta.getColumnCount();int[] data = new int[columeCount];
//獲取字段名跟字段類型for (int i = 1; i < columeCount + 1; i++) {
data[i-1] = rs.findColumn(meta.getColumnName(i));
Log.d("字段名",meta.getColumnName(i));
Log.d("字段類型",meta.getColumnType(i));
}
//根據(jù)字段名獲取該字段的所有數(shù)據(jù)Statement statement = null;
ResultSet result = null;
System.out.println(sql);
List list = new ArrayList();try {
statement = (Statement) conn.createStatement();
result = statement.executeQuery(sql);
if (result != null && result.first()) {
while (!result.isAfterLast()) {
String str = ""; //該處的data就是前面的data,一個for循環(huán)結(jié)束獲取到的是一條完整的數(shù)據(jù)
for(int i:data){ //根據(jù)字段名獲取相應(yīng)的數(shù)據(jù)內(nèi)容
str += result.getString(i)+"\t\t";
}
result.next();
}
}
} catch (SQLException e) {
e.printStackTrace();
}8、新建數(shù)據(jù)庫
使用命令:create database databasename String sql = "create database databasename";query_l(con,sql);
9、刪除數(shù)據(jù)庫
使用命令:drop database databasename String sql = "drop database databasename";query_l(con,sql);
10、刪除數(shù)據(jù)庫中的數(shù)據(jù)表
使用命令:drop table tablename String sql = "drop table tablename";query_l(con,sql);
11、新建數(shù)據(jù)表
使用命令:create table tablename(字段名1 字段類型, 字段名2 字段類型,字段名3...);String sql = "create table tablename(name char(20),sex int(4),number int(4))"; query_l(con,sql); //創(chuàng)建成功后該表中有三個字段:name、sex、number,字段類型分別是:char(20)、int(4)、int(4)
12、清空表中數(shù)據(jù)
使用命令:delete from tablename String sql = "delete from tablename";query_l(con,sql);
13、向表中插入數(shù)據(jù)
使用命令:insert into table value(name1 ,sex1, number1),(name2,sex2,number2),...該處插入數(shù)據(jù)應(yīng)與數(shù)據(jù)表中的字段一一對應(yīng)
String sql = “insert into tablename value('張三',1,1002),('李四',1,1003),('王五',0,1004)”;
query_l(con,sql);14、刪除某一字段
使用的命令:alter table tablename drop 字段名 String sql = “alter table tablename drop sex”;query_l(con,sql); //執(zhí)行完后該表中就沒有sex的字段了
15、更改字段名跟類型
使用命令:alter table tablename change 字段名 新字段名 新字段類型 String sql = "alter table tablename change number age int(4)";query_l(con,sql); //執(zhí)行完后該表中number字段就變成了age
16、重命名表
使用命令:rename table tablename to newtablename String sql = "rename table tablename to newtablename";query_l(con,sql); //執(zhí)行完后就可將原表tablename重命名為newtablename
17、修改數(shù)據(jù)表中的數(shù)據(jù)
使用命令:update tablename set name='aaa' where num=1005命令說明:如果數(shù)據(jù)表中有數(shù)據(jù)num=1005,就將該列數(shù)據(jù)中的name修改成aaa String sql = "update tablename set name='aaa' where num=1005";query_l(con,sql); //如果使用的命令是:update tablename set name='aaa',表示將該表中所有列的數(shù)據(jù)中的name都設(shè)成aaa
提交
醫(yī)療垃圾回收系統(tǒng)工業(yè)平板電腦應(yīng)用解決方案
怎么樣使用QT開發(fā)安卓工業(yè)平板電腦程序
安卓設(shè)備的網(wǎng)絡(luò)adb調(diào)試設(shè)置
安卓工控觸摸一體機的藍牙全面開發(fā)教程
組態(tài)王在WinCE工業(yè)平板電腦的安裝使用

投訴建議