Android的如何能应用程式的存取MySQL的? [英] How can Android apps access MySQL?

查看:126
本文介绍了Android的如何能应用程式的存取MySQL的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何连接mysql数据库中的Java,也可以使用android系统中的一些应用程序?

How can I connect mysql databases in java and use also in android some app?

要与Java DB连接的最好方法,怎么样?

The best way to connect java with db, how?

推荐答案

在他们的机器人是一种具有父类sqlite的里面有所有的数据成员和功能,通过这个class.Through这个类,你可以读访问辅助类,写和开放data.To更多地了解这个阅读此链接

In android their is helper class which has parent class Sqlite which has all the data members and functions to access the through this class.Through this class you can read,write and open data.To know more about this read this link

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

要连接到你需要一个Connection对象的数据库。 Connection对象使用的DriverManager。类DriverManager通过在数据库中的用户名,密码,数据库的位置。

To connect to a database you need a Connection object. The Connection object uses a DriverManager. The DriverManager passes in your database username, your password, and the location of the database.

这三个import语句添加到您的code的顶部:

Add these three import statements to the top of your code:

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

要建立一个数据库的连接时,code是这样的:

To set up a connection to a database, the code is this:

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
   }

这篇关于Android的如何能应用程式的存取MySQL的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆