Hi,

I'm developing an APP for android which requires a database, i'm using mySQL, but haven't had any luck with it's connection, this is my code:

connection class:

public class MySQLConnect extends Activity{
    public void onCreate(Bundle savedInstanceState) {

    }
    public static void connection(String[] args ) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");

        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/prom.pt_test", "root", "root");

        PreparedStatement statement = con.prepareStatement("SELECT * FROM users");
        ResultSet result=statement.executeQuery();

        while(result.next()){
            System.out.println(result.getString(1) + "" + result.getString(2));
        }
    }
}

Android manifest:

<activity
            android:name=".MySQLConnect"
             >

startActivity:

Button testButton = (Button) findViewById(R.id.button1);

        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginScreen.this, MySQLConnect.class));
            }
        });

Am i doing anything wrong???
Any help would be appreciated.

Recommended Answers

All 5 Replies

You should be querying webservice and not directly database. Exposing database to outside world is like telling world "hey here are my user data share it..."

How do i do that?

Just have a server with something like Java servlets listening for url queries. Once received they will run queries and then return JSON object which is easy to parse in Android. Then you can do whatever you want with data (store in local DB, display to user, run calculations etc)

I'm a rookie and we don't understand very much. Can you explain a little better, please?

Thank you

Do you know about Java web development? If yes you know that it has view that is JSP/JSF or other form of visual output controlled by server that listen for user request, forward it to appropriate class for processing and then return some result. This can be acchieved in other frameworks like PHP or .NET too. So when you send request from your phone in the form like http://www.somewebsite.com/user?userId=1234 your servlet listening for http://www.somewebsite.com/user will receive this request, it will know that if you call on this and provide some parameters it is supposed to query database table user with where parameter of userId=1234. Once data retrieve convert it to JSON format and return to servlet that will send response to user that request it.
If you donot know any server side methodology you have small chance of success unfortunately.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.