Android中Activity中访问数据库操作记录

Posted 维也纳的忧伤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中Activity中访问数据库操作记录相关的知识,希望对你有一定的参考价值。

public class MainActivity extends AppCompatActivity {

    String UserName = "hhh";//用户名
    String Password = "137006";//密码
    Connection con = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView viewById1 = (TextView) findViewById(R.id.tv_btn1);
        TextView viewById2 = (TextView) findViewById(R.id.tv_btn2);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        viewById1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try { // 加载驱动程序
                            Class.forName("com.mysql.jdbc.Driver");
//                            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test1", UserName, Password);

                            con = DriverManager.getConnection("jdbc:mysql://192.168.1.97:3306/Test1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false", UserName, Password);

                        } catch (ClassNotFoundException e) {
                            System.out.println("加载驱动程序出错");
                        } catch (SQLException sql) {
                            System.out.println("SQLException: " + sql.getMessage());
                            System.out.println("SQLState: " + sql.getSQLState());
                            System.out.println("Erro: " + sql.getErrorCode());
                            System.out.println("StackTrace: " + sql.getStackTrace());
                            System.out.println(sql.getMessage());
                        } catch (Exception e) {
                            System.out.println(e.getMessage());

                        }
                    }
                }).start();

 

            }
        });

        viewById2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (con != null) {
                    try {
                        testConnection(con);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }

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

        try {

            String sql = "SELECT * FROM GoodsInfo";//查询表名为“table_test”的所有内容
            Statement stmt = con.createStatement();//创建Statement
            ResultSet rs = stmt.executeQuery(sql);//ResultSet类似Cursor

            while (rs.next()) {//<code>ResultSet</code>最初指向第一行
                System.out.println(rs.getString("amount"));//输出第n行,列名为“test_id”的值
                System.out.println(rs.getString("spec"));

            }

            rs.close();
            stmt.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage().toString());
        } finally {
            if (con != null)
                try {
                    con.close();
                } catch (SQLException e) {
                }
        }
    }
}

以上是关于Android中Activity中访问数据库操作记录的主要内容,如果未能解决你的问题,请参考以下文章

Android开发中需要注意哪些坑

Android Activity标签属性

Android从activity2访问activity1

Activity

Android activity等待service数据后再执行

Android中Activity之间的数据传递ngti