1

i was trying to connect and application with a free server, tried using web service but couldnt do it at all, so now i am trying to do it using jdbc :

the code :

@Override
protected void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    getActionBar().hide();

    new Con().execute();}

    private class Con extends AsyncTask<Void, Void, Void> {

    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://31.170.165.253/u802222393_gp";

    // Database credentials
    static final String USER = "u802222393_books";
    static final String PASS = "aboasendar";

    @Override
    protected Void doInBackground(Void... params) {
        Connection conn = null;
        Statement stmt = null;

        try {
            // STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER).newInstance();




            // STEP 3: Open a connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql= "SELECT id, first, last, age FROM test";
            ResultSet rs = stmt.executeQuery(sql);

            // STEP 5: Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                int id = rs.getInt("id");
                int age = rs.getInt("age");
                String first = rs.getString("first");
                String last = rs.getString("last");

                // Display values
                System.out.print("ID: " + id);
                System.out.print(", Age: " + age);
                System.out.print(", First: " + first);
                System.out.println(", Last: " + last);
            }
            // STEP 6: Clean-up environment
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            }// nothing we can do
            catch (java.sql.SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }// end finally try
            catch (java.sql.SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }// end try
        System.out.println("Goodbye!");

        return null;
    }

it always gives an exception on the URL i searched alot but didnt find the answer on how i am supposed to write the URL

Logcat :enter image description here

5
  • did you try exporting data from database with php into json array and then use the JSONObject to read them in your device?? Commented Jan 23, 2015 at 11:30
  • this project is for the university and they want us to use jdbc Commented Jan 23, 2015 at 11:32
  • Wherever your code is placed it could not reach the destination address. I took a look on the service you're using and it seems that you cannot access MySQL database from outside. Portuguese link: hostinger.com.br/base-de-conhecimento/5.html Commented Jan 23, 2015 at 11:34
  • on hostinger forget about it when you are free user. maybe you can do it with localhost but you gonna have to bring the laptop to university.. i had the same problemm with Creating an app in netbeans trying to do the same thing from free user at hostinger....i would not reccomend it Commented Jan 23, 2015 at 11:36
  • so there is nothing free , thank you very much for your feedback Commented Jan 23, 2015 at 11:40

1 Answer 1

1

you can use localhost (use your computer as server) and do work as you go for using JDBC in android you will find Good guide here, http://www.vogella.com/tutorials/MySQLJava/article.html

if you want to start from jdbc the yuou may start from this Tutorial http://www.tutorialspoint.com/jdbc/jdbc-introduction.htm

if you have any question , Please comment thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.