简体   繁体   中英

Problem connect Android Studio-MySQL using JDBC (Error : java.sql.SQLNonTransientConnectionException: Could not create connection to database server.)

I want create a connection between android studio and MySql.

Version of MySQL = MySQL Workbench 8.0 CE

Version of MySQL connector = mysql-connector-java-8.0.23.jar

Name of my database = testandroid

I already added the connector to my project

I got the Error: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.

Code:

private Connection getConnection(){
        Connection con;
        try{
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testandroid?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT","root","");
            return con;
        }catch (Exception e){
            System.out.println("Error ************************"+e);
            return null;
        }
    }


    private ArrayList<User> getUserList()
    {
        ArrayList<User> userList = new ArrayList<>();
        Connection connection = getConnection();


        String query = "Select pseudo FROM `users` where userID="+1;
        Statement st;
        ResultSet rs;

        try{
            st =  connection.createStatement();
            rs = st.executeQuery(query);
            User user ;
            while (rs.next())
            {
                user = new User(rs.getString("pseudo"));
                userList.add(user);
            }

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

        return userList;
    }


    private void showData(){

        ArrayList<User> list = getUserList();

        for (int i=0; i<list.size(); i++){

            test.setText(list.get(i).getPseudo());
            test.setText("fnpvrpogn");

        }

    }

The Error

I/System.out: Error ************************java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
        at com.example.testjdbc.MainActivity.getUserList(MainActivity.java:54)
W/System.err:     at com.example.testjdbc.MainActivity.showData(MainActivity.java:73)
W/System.err:     at com.example.testjdbc.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:7136)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

For software running on an Android device or emulator, localhost is the Android device or emulator. Most likely, your MySQL instance is not running on your Android device or emulator.

If you are running your app on an Android device, you need to use the IP address or host name of the machine running MySQL.

If you are running your app on an emulator, and MySQL is running on the same computer as is the emulator, use 10.0.2.2 as the address instead of localhost .


Also, please note that JDBC is not designed for use from mobile devices, from a reliability and security standpoint. Please consider doing something else, such as having your app talk to a Web service, which in turn talks to the database.

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