android studio通过jdbc接口连mysql

package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.myapplication.URL.DBConnection;

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

public class MysqlActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(runnable).start();

    }
    Handler myHandler=new Handler(){

        public void handleMessage(Message msg) {

            // TODO Auto-generated method stub

            super.handleMessage(msg);

            Bundle data=new Bundle();

            data=msg.getData();

            //System.out.println("id:"+data.get("id").toString());  //输出第n行,列名为“id”的值

            Log.e("TAG","id:"+data.get("id").toString());


            //System.out.println("content:"+data.get("content").toString());

        }

    };
    Runnable runnable=new Runnable() {

        private Connection con = null;

        @Override

        public void run() {

            // TODO Auto-generated method stub

            try {

                Class.forName("com.mysql.jdbc.Driver");

                //引用代码此处需要修改,address为数据IP,Port为端口号,DBName为数据名称,UserName为数据库登录账户,Password为数据库登录密码

                //模拟器默认把127.0.0.1和localhost当做本身了,在模拟器上可以用10.0.2.2代替127.0.0.1和localhos
                con =DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/qc","root", "");
                System.out.println("数据库连接对象"+con);

            } catch (SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            } catch (ClassNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            try {

                testConnection(con);  //测试数据库连接

            } catch (java.sql.SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

        public void testConnection(Connection con1) throws java.sql.SQLException {

            try {
                String sql = "select * from test_sht";    //查询表名为“oner_alarm”的所有内容
                Statement stmt = con1.createStatement();    //创建Statement

                ResultSet rs = stmt.executeQuery(sql);     //ResultSet类似Cursor

                //<code>ResultSet</code>最初指向第一行

                Bundle bundle=new Bundle();

                while (rs.next()) {

                    bundle.clear();

                    bundle.putString("id",rs.getString("data5"));

                    //bundle.putString("content",rs.getString("content"));

                    Message msg=new Message();

                    msg.setData(bundle);

                    myHandler.sendMessage(msg);

                }

                rs.close();

                stmt.close();

            } catch (SQLException e) {

            } finally {

                if (con1 != null)

                    try {

                        con1.close();

                    } catch (SQLException e) {
                        
                    }

            }

        }

    };
}

上一篇:java.sql.SQLException: ORA-00942: 表或视图不存在


下一篇:jdbc--Mysql异常问题总结