Android uses JDBC to connect to mysql database

Respect originality: http://blog.csdn.net/yuanzeyao/article/details/38777557

The database commonly used in mobile devices is the sqlite database, and Android devices are no exception, but today we will discuss how to use Android to connect to


the mysql database. In actual projects, it seems that android is rarely used to connect to the mysql database, usually in the Android client. The data is sent to the server through Http requests, and then the server connects to the mysql database. Before learning how to connect android to Mysql database, let's review the six steps of
how java connects to Mysql JDBC program through jdbc : 1. Registering the driver There are three ways to register the driver: Method 1: Class.forName("com.mysql .jdbc.Driver"); The JAVA specification clearly stipulates that all drivers must register the driver with the driver manager in the static initialization code block. Method 2: Driver drv = new com.mysql.jdbc.Driver();       DriverManager.registerDriver(drv); Method 3: Load the driver java in the virtual machine at compile time –Djdbc.drivers = com.mysql.jdbc.Driver xxx. java java –D jdbc.drivers=The full name of the driver class name uses the system property name, loading the driver -D means assigning a value to the system property 2. Establish a database connection object (Connection) Method 1:













Connection conn=DriverManager.getConnection("jdbc:mysql://192.168.8.21:3306/test", "User","Pasword");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1 :1521:ORCL","scott","scott");
Method 2:
Properties pro = new Properties();
pro.put("user",userName);
pro.put("password",password);
Connection con = DriverManager.getConnection(url,pro);
3. Create the Statement object
Statement stm = con.createStatement();
4. Send the SQL statement
stm.executeUpdate() or stm.executeQuery()
5. If there is a result set, process the result set (ResultSet)
6. Close the corresponding stream resource,


then start connecting to the Mysql database through Jdbc in android

public void onClickQuery(View view)  
 {  
//Operating the database in android is best performed in a child thread, otherwise an exception may be reported  
   new Thread()  
   {  
     public void run() {  
       try {  
      //注册驱动  
         Class.forName("com.mysql.jdbc.Driver");  
         String url = "jdbc:mysql://10.0.2.2:3306/gjun";  
         Connection conn = DriverManager.getConnection(url, "root", "gavin");  
         Statement stmt = conn.createStatement();  
         String sql = "select * from t_user";  
         ResultSet rs = stmt.executeQuery(sql);  
  
  
         while (rs.next()) {  
           Log.v("yzy", "field1-->"+rs.getString(1)+"  field2-->"+rs.getString(2));  
         }  
  
  
         rs.close();  
         stmt.close();  
         conn.close();  
         Log.v("yzy", "success to connect!");  
       }catch(ClassNotFoundException e)  
       {  
         Log.v("yzy", "fail to connect!"+"  "+e.getMessage());  
       } catch (SQLException e)  
       {  
         Log.v("yzy", "fail to connect!"+"  "+e.getMessage());  
       }  
     };  
   }.start();  
     
 }  

 注意,我这个是在模拟器上运行的,如果在真机上运行,那么Ip地址就需要换成真实Ip地址,在虚拟机中,10.0.2.2指的就是电脑的Ip 地址

  
  在运行过程中一定要注意:
  1、关闭防火墙
  2、在配置文件中加入网络访问权限android.permission.INTERNET ,不然一直失败
  3、mysql数据库运行远程用户访问(如何设置请到网上查)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326437152&siteId=291194637