通过Android错误连接MySQL

时间:2017-11-28 12:56:04

标签: java android mysql jdbc

我的Android项目有点问题。 我想用MySQL数据库中的数据填充列表,但是android不会连接到它。

这是我的代码:

  

MySQLConnector:

private static Connection con = null;

private static String dbHost = "85.214.101.213"; // changed the ip :)
private static String dbPort = "3306"; // Port -- Standard: 3306
private static String dbName = "ticketsystem"; // Datenbankname
private static String dbUser = "admin"; // Datenbankuser
private static String dbPass = "root"; // Datenbankpasswort

public static void conect() {
    try {
        Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC

        System.out.println("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + "?" + "user="
                + dbUser + "&" + "password=" + dbPass);

        con = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + "?" + "user="
                + dbUser + "&" + "password=" + dbPass);




    } catch (ClassNotFoundException e) {
        System.out.println("Treiber nicht gefunden");
    } catch (SQLException e) {
        System.out.println(e);
        System.out.println("Verbindung nicht moglich");
        System.out.println("SQLException: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("VendorError: " + e.getErrorCode());
    }


}
  

SQLManager:

public SQLManager() {
    MySQLConnection.conect();
    Log.e("test", "nach conect");
    con = MySQLConnection.getCon();
}


public String[] getFreeEvents() {

    ArrayList<String> mList = new ArrayList();

    try
    {
        PreparedStatement ps = con.prepareStatement(" SELECT `EventID`,`Name`,`Zeit`,`Ort` FROM `event` WHERE `freeTickets` >= ?");

        ps.setInt(1, 0);
        ResultSet rs = ps.executeQuery();
        while (rs.next())
        {
            int EventID = rs.getInt("EventID");
            String Name = rs.getString("Name");
            String Zeit = rs.getString("Zeit");
            String Ort = rs.getString("Ort");

            String info = Name +"," + Zeit + ","+Ort  ;

            mList.add(info);
            System.out.println(info);
        }


    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    String[] array = mList.toArray(new String[0]);
    return array;
}
  

该类形成Android活动:

Button b;
ListView list;
EditText vor;
TextView nach;
SQLManager man = new SQLManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_ticket);
    setContentView(R.layout.activity_main);
    b = findViewById(R.id.button);
    b.setOnClickListener(this);
    list = findViewById(R.id.list);
    list.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, man.getFreeEvents())); //here is the SQL request...
    vor = findViewById(R.id.editText);
    nach = findViewById(R.id.editText2);
}

我的猜测是Android无法打开连接。 我在Android Manifest中添加了Permission请求:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

所以至少我得到了这个错误:

I/System.out: `com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.´
I/System.out: Verbindung nicht moglich
I/System.out: SQLException: Could not create connection to database server.
I/System.out: SQLState: 08001
I/System.out: VendorError: 0
E/test: nach conect
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ml.gottfried.myapp, PID: 10440
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ml.gottfried.myapp/ml.gottfried.myapp.NewTicket}: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
                      at ml.gottfried.myapp.SQLManager.getFreeEvents(SQLManager.java:82)
                      at ml.gottfried.myapp.NewTicket.onCreate(NewTicket.java:43)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我希望你能帮助我

谢谢:)

PS:如果你有任何问题,请问他们:)

1 个答案:

答案 0 :(得分:0)

所以我尝试了许多想法,这对我有所帮助:

添加

     StrictMode.ThreadPolicy policy = new`StrictMode.ThreadPolicy.Builder().permitAll().build();
     StrictMode.setThreadPolicy(policy);

到MySQLConnector连接方法并添加

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >  </uses-permission>

到Android清单。

现在我的设备和数据库之间的连接正常工作:)