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){
开发者_C百科
}
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
...
精彩评论