简体   繁体   中英

What's wrong with my sql connection?

I've created a web service to connect my android device to a mysql database. I made the web service in java using eclipse galileo. I know the web service is working well because I can get my method to return a string so, the only possible problem is with my sql query:

      public String getUsers()

{

        String username = "root";

        String password = "ticket";

        String tablename = "users";

        String fieldname = "*";

                    String query = "SELECT " + fieldname + " FROM " + "android." + tablename + ";";

        /* this chnk of code can appear more or less verbatim */
        /* in your database apps (including JSPs) */
        String url = "jdbc:mysql://"my IP address":3306/android";

        try{

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, username, password);
        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()){

        }
        rs.close();

        stmt.close();

        con.close();

        }catch(Exception e){

        }

        return test;
        }

Through debugging: I've narrowed the fail point down to :

Connection con = DriverManager.getConnection(url, username, password);

But, I can't figure out why I am getting the error.

I know my "mysql username and password is correct": I got my url from eclipse's database development perspective when I created the connection. Yes, I have tried switching from my IP address to "localhost" and vice versa. It's been a few hours now, and maybe I just need a fresh set of eyes. Any ideas would be much appreaciated.

Thanks.

If you get a java.lang.ClassNotFoundException , then you have to be sure that you put your MySQL driver on the class path of your web service implemenation. In Eclipse, this can be done quite easily (I can't tell you how exactly, as I do not know your exact setup). Try googling eclipse classpath ...

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