File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Connecting to mysql using java

 
Karan Raj
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As a part of project i wrote the code to connect to mysql using java.In my system it is working fine.but when i tried on the server(even as root also) i am getting an error message.I am not able to sort out the what the problem could be..?
mycode is
{
...................................
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch (Exception E) {
System.err.println("CONCEPT: Unable to Load Driver ");
E.printStackTrace();
}
try {
System.out.println("Connecting...");
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost/mysql?user=samurai&password=helloworld");
System.out.println("Connection ok: " + c);
System.out.println("Creating BankID table:");
Statement stmt=c.createStatement();
stmt.executeUpdate("use SST");
stmt.executeUpdate("CREATE TABLE BANKID (EID VARCHAR(20) UNIQUE)");
System.out.println("Table with following Attributes is created:");
ResultSet res=stmt.executeQuery("DESCRIBE BANKID");
while(res.next()){
String s=res.getString("Field");
System.out.println("-->"+s);
s=res.getString("Type");
System.out.println("-->"+s);
}
}catch (SQLException E) {
System.out.println ("SQLException: " + E.getMessage());
System.out.println ("SQLState: " + E.getSQLState());
System.out.println ("VendorError: " + E.getErrorCode());
}
}
public void addGID(String name){

String Query;
Query="INSERT INTO TABLE BANKID VALUES ( "+name +" )";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch (Exception E) {
System.err.println("CONCEPT: Unable to Load Driver ");
E.printStackTrace();
}..................
..............................

The above code is working fine in mysystem.but it's giving the following error when i try to run it on other system.
Error Message:
SQL Error: Data Source rejected for the establishment of the connection.user 'Host'@localhost is not allowed to connect to MySql Server.

Vendor Error Code: 1130

please help me

Thanks,
karan
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may need to update the server to allow the user to connect from that location, but I didn't think it was problem when connecting from the local machine.
Try this:

Note that you'll have to substitute for the database, username etc.
eg GRANT SELECT ON mysql.* TO ...
I'm confused why it is refusing 'HOST' when you're connecting with 'samurai' though...
 
Karan Raj
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already gave all privileges to user 'samurai'..
mysql> GRANT ALL PRIVILEGES ON SST.* TO 'samurai' IDENTIFIED BY 'helloworld';
i am able to access all tables in SST with samurai user manually. but i am getting error when i tried with my java code.I am not able to understand why it is giving ".... 'HOST '@localhost is not allowed to connect to MySql".
I tried the code even on the server system but it's giving same kind of error.
 
Varun Khanna
Ranch Hand
Posts: 1400
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you are not using "localhost" while making a remote connection.
Use IP address of the server instead of "localhost".
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic