MySQL Forums
Forum List  »  Connector/JDBC and Java

connect to MYSQL with servlet
Posted by: Mads Kristensen
Date: July 09, 2013 06:29AM

Hello.

Okay I have been struggeling with a problem the past two days, and I Cant figure why, so I am pretty much stock. I talked to some people, and they dont know what is wrong either. I want to connect my servlet to a MYSQL database, and I am using MAMP.
This is my code:


import java.sql.Connection;
import java.sql.DriverManager;

public class TestConnection {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Loaded driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db", "root", "root");
System.out.println("Connected to MySQL");
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}

Screenshot of my eclipse: http://postimg.org/image/fevv519l9/
Ind the left side I have my mysql connector.

My database is called db, and in my database i made two tables: username, and password. Sceenshot is here: http://postimg.org/image/nrlzijhy7/

So when I run this servlet I get a 404 error. The screenshot of that is here:
http://postimg.org/image/u8u03pe79/

In my servlet I also tried to put this in the con, but with the same result:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db1", "root", "root");

and

Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/db1", "root", "root");

The reason I use servlets is because it is an university assigment, where we have to use servlet. I really hope someone can help, because it is driving me crazy :-/ So does anyone out there have a suggestion to what I have to do, to connect to MYSQL database with this servlet?

Best Regards
Mads

Options: ReplyQuote


Subject
Written By
Posted
connect to MYSQL with servlet
July 09, 2013 06:29AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.