0

I have a mysql database running on a wamp server, I want to make a connection to mysql database using a Servlet Class in Java inorder to access the data in the database how can i do this.

private static final String url = "jdbc:mysql://192.168.1.6:3306/hospital_mangement";
private static Connection con;



 static {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection(url, "", "");
    } catch (Exception e) {
        e.printStackTrace();
    }

This is the way i tried to connect to the mysql database running on the wamp server but it doesnt seem to work.

When i run this program and try to access the database in my Apache tomcat log the following error appears.

java.sql.SQLException: null, message from server: "Host 'test-PC' is not allowed to connect to this MySQL server"

Marlon Fernando
  • 29
  • 1
  • 3
  • 7
  • Have you tried anything, if yes then please share your code. – Braj Jun 14 '14 at 18:37
  • The title of this post and actual statement is some what different. In title you are talking about JSP and in statement you are talking about Scriplet. – Braj Jun 14 '14 at 18:38
  • It might help you. Read [ClassNotFoundException com.mysql.jdbc.Driver](http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver) – Braj Jun 14 '14 at 19:01
  • I am sorry i have not added the library file properly once it is done the following error is shown java.sql.SQLException: null, message from server: "Host 'test-PC' is not allowed to connect to this MySQL server" – Marlon Fernando Jun 14 '14 at 19:06
  • There is no need to call `newInstance();`. – Braj Jun 14 '14 at 19:16

1 Answers1

0

I think this is due to permission problem.As you are using IP address 192.168.1.6 so I think you are trying to connect from remote system as your URL String is

 private static final String url = "jdbc:mysql://192.168.1.6:3306/hospital_mangement";

If yes then you have grant permission for that.This is an example

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%192.168.1.%' 
    IDENTIFIED BY PASSWORD 'password' 
    WITH GRANT OPTION;
FLUSH PRIVILEGES;

source

If no then just try

private static final String url = "jdbc:mysql://localhost:3306/hospital_mangement";
rocking
  • 4,729
  • 9
  • 30
  • 45
  • I got it working like you have said it was an issue with the privileges since i am accessing from a different machine i had to add a new user in the wamp server with the details about my other machine, after setting up the user now its working. Thank you very much for your quick response. – Marlon Fernando Jun 14 '14 at 19:41