【问题标题】:How can Android apps access MySQL? [closed]Android 应用程序如何访问 MySQL? [关闭]
【发布时间】:2013-10-21 04:08:19
【问题描述】:

如何在java中连接mysql数据库并在android中使用一些应用程序?

java与db连接的最佳方式,如何?

【问题讨论】:

    标签: java android mysql


    【解决方案1】:

    在 android 中,它们是辅助类,它具有父类 Sqlite,它具有通过该类访问的所有数据成员和函数。通过该类,您可以读取、写入和打开数据。要了解有关此的更多信息,请阅读此链接

    http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android
    

    要连接到数据库,您需要一个 Connection 对象。 Connection 对象使用 DriverManager。 DriverManager 传入您的数据库用户名、密码和数据库位置。

    将这三个导入语句添加到代码的顶部:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    

    要建立与数据库的连接,代码如下:

    Connection con = DriverManager.getConnection( host, username, password );
    

    看这个例子

    try (
             // Step 1: Allocate a database "Connection" object
             Connection conn = DriverManager.getConnection(
                   "jdbc:mysql://localhost:8888/ebookshop", "myuser", "xxxx"); // MySQL
    //       Connection conn = DriverManager.getConnection(
    //             "jdbc:odbc:ebookshopODBC");  // Access
    
             // Step 2: Allocate a "Statement" object in the Connection
             Statement stmt = conn.createStatement();
          ) {
             // Step 3: Execute a SQL SELECT query, the query result
             //  is returned in a "ResultSet" object.
             String strSelect = "select title, price, qty from books";
             System.out.println("The SQL query is: " + strSelect); // Echo For debugging
             System.out.println();
    
             ResultSet rset = stmt.executeQuery(strSelect);
    
             // Step 4: Process the ResultSet by scrolling the cursor forward via next().
             //  For each row, retrieve the contents of the cells with getXxx(columnName).
             System.out.println("The records selected are:");
             int rowCount = 0;
             while(rset.next()) {   // Move the cursor to the next row
                String title = rset.getString("title");
                double price = rset.getDouble("price");
                int    qty   = rset.getInt("qty");
                System.out.println(title + ", " + price + ", " + qty);
                ++rowCount;
             }
             System.out.println("Total number of records = " + rowCount);
    
          } catch(SQLException ex) {
             ex.printStackTrace();
          }
          // Step 5: Close the resources - Done automatically by try-with-resources
       }
    

    【讨论】:

    • 我得到错误:内部错误:**(java.io.FileNotFoundException)/home/nasimjon/IdeaProjects/Games/.idea/misc.xml(没有这样的文件或目录)
    • 是的,我已连接。问题是我没有在库 mysql 连接器 j 中打补丁。所以现在没问题,但是 stile 需要 android 方面的帮助。
    【解决方案2】:

    从 Android 设备连接到远程 MySQL 数据库最常见的方法是在中间放置某种服务。由于 MySQL 通常与 PHP 一起使用,因此最简单和最明显的方法是 write a PHP script 管理数据库并使用 HTTP 协议运行此脚本 从 Android 系统。

    您可以参考:Connect to MySQL using PHP on android 作为开始。

    附加说明:Java 传统上使用 JDBC 连接来管理数据源。有许多可用的框架可以更有效地管理这一点。这些框架使编写数据访问代码更容易,并且比传统的 JDBC 代码更容易管理。此类框架也可用于 Android。寻找他们。我相信你会找到一些答案。 :)

    【讨论】:

      【解决方案3】:

      在服务器端使用php连接和维护MySql数据库,然后你可以使用一些服务从Android执行那个php脚本。如果你想知道如何连接PHP MySql和Android这里是一个例子http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/这个使用wamp/lamp 服务器。

      如果你想在应用程序中创建一个数据库,你可以使用 SqLite 数据库。当您的应用程序需要维护内部数据库时,这非常有用。这是一个说明 Sqlite 使用的示例http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-16
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        • 2017-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多