简体   繁体   中英

MySQL connection via Android error

I have a little Problem with my Android Project. I want to fill a list with data from my MySQL database, but android would not connect to it.

Here's my code:

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;
}

And the Class form the Android activity:

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);
}

My probem is that Android could not open a connction. I have added the Permission request in the Android Manifest:

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

So at least I got this error:

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) 

I hope you can help me

Thanks :)

PS: If you have any quesstions ask them :)

so i tried many thinks and this helped me:

add

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

to the MySQLConnector to the connect methode and add

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

to the Android manifest.

Now the connection between my device and my database works :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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