简体   繁体   English

MySQL数据库连接不起作用

[英]MySQL database connection not working

First time using an external database with android and i'm trying to set up the database connections. 第一次在Android中使用外部数据库,而我正在尝试建立数据库连接。 I created a basic table in the database just for testing purposes and am trying to connect and insert some data. 我在数据库中创建了一个基本表,仅用于测试目的,并且试图连接和插入一些数据。 This is my code: 这是我的代码:

public class DBInterface {
//TODO: Fix the database connections

    private Connection conn = null;
    private  Statement statement = null;



    public void connectToDatabase(){
        Log.d("myTag", "This is my messagestart");
        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://mysql.**.***.***.***:3306", "DB_USER", "DB_PASSWORD");
            statement = conn.createStatement();
            statement.executeUpdate("INSERT INTO test (test) VALUES (15)");
            Log.d("myTag", "This is my message");


        } catch (SQLException ex) {
            Log.d("myTag", "SQLException: " + ex.getMessage());
            Log.d("myTag","SQLState: " + ex.getSQLState());
            Log.d("myTag","VendorError: " + ex.getErrorCode());
        }catch(Exception e){e.printStackTrace();}
    }

and I call it in the onCreate of the MainActivity: 我在MainActivity的onCreate中调用它:

         DBInterface db = new DBInterface();
         db.connectToDatabase();
         Log.d("myTag", "This is my message2");

No data is inserted but i also don't get any errors, i put the log messages in to try and see where it was getting to and the only output to the log is: 没有插入任何数据,但我也没有收到任何错误,我将日志消息放入其中以尝试查看其到达的位置,并且日志的唯一输出是:

10-31 16:13:41.481 9918-9918/com.jacksteel.comp4 D/myTag: This is my messagestart 10-31 16:13:41.481 9918-9918 / com.jacksteel.comp4 D / myTag:这是我的messagestart

10-31 16:13:41.486 9918-9918/com.jacksteel.comp4 D/myTag: This is my message2 10-31 16:13:41.486 9918-9918 / com.jacksteel.comp4 D / myTag:这是我的消息

Not a direct answer, but you never want to connect your app directly to your database. 这不是一个直接的答案,但是您永远不想将应用程序直接连接到数据库。 Instead you should write some scripts on your server to perform INSERT, SELECT, UPDATE , DELETE. 相反,您应该在服务器上编写一些脚本来执行INSERT,SELECT,UPDATE和DELETE。

Your app will then send requests to these scripts to execute the queries. 然后,您的应用程序将向这些脚本发送请求以执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM