Hi, I have been looking around to see if i can find a way to connect to my database and retrieve some information. I did get some good code examples but when I sum them together it doesen't seem to work. Can you figure out what I'm doing wrong? Code: Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql://" + "localhost", "root", "password"); //con is a nill value of type "Connection" PreparedStatement prepareStatement = con.prepareStatement("use " + "hello"); prepareStatement.executeQuery(); Note: I have added the mysql connector as a reference from their official site.
Hi, my guess is you forgot to connect to a DB: String userName = "testuser"; String password = "testpass"; String url = "jdbc:mysql://localhost/test"; // connection on Localhost and DB test Class.forName ("com.mysql.jdbc.Driver").newInstance (); conn = DriverManager.getConnection (url, userName, password);
Darn! I knew it was something I forgot to include! Okay, now the Class.forName line gives me a "ClassNotFoundExeption", a "IllegalAccessExeption" and a "InstantiationExeption" and the other three lines gives me a "SQLExeption" probably because of the error in the first line. Well, isn't that code doing exactly the same as mine?
no Code: con = DriverManager.getConnection("jdbc:mysql://" + "localhost", "root", "password") there is no "/DBname" (test) at his code or i don't see it there. Code: String userName = "testuser"; String password = "testpass"; String url = "jdbc:mysql://localhost/---->!!!!test<----"; // <<<---- there is something diffrent about that i guess Class.forName ("com.mysql.jdbc.Driver").newInstance (); conn = DriverManager.getConnection (url, userName, password); behind the localhost should be the name of the database you are trying to connect to. else the connection wouldn't make any sense at alle because a DBserver can hold many DBs for a variaty of users.
Ahh, okay, I didn't really notice that, sorry. Anyways, I decided to make a plugin that connects to a C# program through a TCP connection, that way I get to work more with a language and IDE I'm familiar with. It might not be to efficient, but it will be integrated into my server management application so it doesn't really matter. So thank you for your time and help, and cya later!