0

I want to connect to my MySQL server, but the driver doesn't work. How can I fix this, or what am I doing wrong? Do I need to import extra libraries (I didn't do that)?

This is my code:

public void Connection() {

    String retrievedUserName = "";
    String retrievedPassword = "";
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://domainexample.com", "username", "passwoord");
        PreparedStatement statement = con.prepareStatement("SELECT * FROM Gebruiker WHERE users= '" + "username" + "'");
        ResultSet result = statement.executeQuery();

        while (result.next()) {
            retrievedUserName = result.getString("gebruikersnaam");
            retrievedPassword = result.getString("password");
        }
        System.out.println(retrievedUserName + " passwoord = " + retrievedPassword);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
vanlooverenkoen
  • 2,121
  • 26
  • 50
  • you added the jar to your project build path and set the permissions? – nano_nano Dec 15 '15 at 08:00
  • I can't get it to work, or don't know how to add a jar to android studio? – vanlooverenkoen Dec 15 '15 at 08:01
  • I am working with Eclipse. Search for "how to add librarys in Android Studio" – nano_nano Dec 15 '15 at 08:03
  • i have added this: "compile files('libs/mysql-connector-java-5.1.38-bin.jar')" to my build.gradle -> then I got 2 extra errors – vanlooverenkoen Dec 15 '15 at 08:11
  • woeps, indeed, that is what I meant. But why doesn't it work? I don't get it, I'm new to this. – vanlooverenkoen Dec 15 '15 at 08:22
  • http://i66.tinypic.com/mtw2fp.png this are the errors I got – vanlooverenkoen Dec 15 '15 at 08:25
  • http://i63.tinypic.com/9hhnjn.png this is my project structure – vanlooverenkoen Dec 15 '15 at 08:26
  • I use android studio. And to import a jar you need to do different. when I build my project it is OK. but when I run my program i get 2 erros. i66.tinypic.com/mtw2fp.png – vanlooverenkoen Dec 15 '15 at 08:40
  • Alright. Did you take a look at [this](http://stackoverflow.com/questions/7221620/android-jdbc-not-working-classnotfoundexception-on-driver)? – Ivar Dec 15 '15 at 08:45
  • Aah oke, so this is not realy the right way? So I need to create a webapplet arround the database? or with php? something like this? -> http://codeoncloud.blogspot.in/2013/07/android-mysql-php-json-tutorial.html – vanlooverenkoen Dec 15 '15 at 08:51
  • That indeed looks like the way to go. One more tip: It looks like you are storing your passwords as plain text in your database. You should never ever do that. You should hash your password, and if someone tries to login, hash that password as well, and compare the hashes. (Take a look at [this OWASP page](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet).) – Ivar Dec 15 '15 at 08:56
  • Yes I know :) this is just my test data, I first want to make shure that I can connect to the database an get everything working without security :) but I will indeed change this with a hash. Thanks anyway ;) – vanlooverenkoen Dec 15 '15 at 09:02

0 Answers0