简体   繁体   中英

Connecting android with a database using JDBC

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 : 在此处输入图片说明

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

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