使用E4A(Easy4Android)開發Android應用將數據存儲到數據庫中,通常采用MySQL數據庫。下面介紹E4A如何與MySQL數據庫進行連接。
' 導入相應的類庫 import java.sql.Connection import java.sql.DriverManager import java.sql.ResultSet import java.sql.Statement ' 定義連接信息 dim serverip as string = "192.168.1.2" dim username as string = "root" dim password as string = "123456" dim dbname as string = "test" Dim connectionString As String = "jdbc:mysql://" & serverip & "/" & dbname & "?user=" & username & "&password=" & password & "&useUnicode=true&characterEncoding=UTF8" ' 建立連接 Dim conn As Connection = DriverManager.getConnection(connectionString) ' 執行SQL語句 Dim statement As Statement = conn.createStatement Dim sql As String = "select * from student" Dim rs As ResultSet = statement.executeQuery(sql) ' 處理結果集 While rs.next Dim id As Int = rs.getInt("id") Dim name As String = rs.getString("name") Dim age As Int = rs.getInt("age") ' 其他操作... End While ' 關閉連接 rs.Close statement.Close conn.Close
以上代碼中,我們使用了Java提供的JDBC(Java Database Connectivity)進行連接MySQL數據庫,并執行相應的SQL語句,最后關閉連接。其中,需要填寫正確的連接信息,包括MySQL數據庫的IP地址、用戶名、密碼和數據庫名稱。