LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-28-2005, 02:13 PM   #1
deepanvenkatesh
LQ Newbie
 
Registered: Oct 2005
Location: Bangalore
Posts: 19

Rep: Reputation: 0
Help me in connecting Java and Mysql


Hi All,

Im trying to connect the java and mysql.Im using the code,

[CODE]
import java.sql.*;

public class TestMysql
{
public static void main(String args[]) {
try {
/* Test loading driver */

String driver = "com.mysql.jdbc.Driver";

System.out.println( "\n=> loading driver:" );
Class.forName( driver ).newInstance();
System.out.println( "OK" );

/* Test the connection */

String url = "jdbc:mysql://localhost:3306/<dir>";

System.out.println( "\n=> connecting:" );
DriverManager.getConnection( url, "user", "password" );

System.out.println( "OK" );
}
catch( Exception x ) {
System.err.println( x );
}
}
}


When i run this java file, im getting a mysql error.The error is

java.sql.SQLException: Server configuration denies access to data source

I gussed that it may be a privilege error.So i had issued the commands in mysql.

GRANT ALL PRIVILEGES ON *.* TO user@localhost IDENTIFIED BY "password" WITH GRANT OPTION;

FLUSH PRIVILEGES;

But the problem is still, im getting the same error.Will anybody help me on this?

Thanks a lot.
 
Old 10-28-2005, 03:24 PM   #2
InJesus
Member
 
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33

Rep: Reputation: 15
try this

mysql> GRANT ALL PRIVILEGES ON *.* TO username@localhost
IDENTIFIED BY 'password' WITH GRANT OPTION;


Connection connection = null;
try {
// Load the JDBC driver
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);

// Create a connection to the database
String serverName = "localhost";
String mydatabase = "mydatabase";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}
 
Old 10-29-2005, 05:17 AM   #3
deepanvenkatesh
LQ Newbie
 
Registered: Oct 2005
Location: Bangalore
Posts: 19

Original Poster
Rep: Reputation: 0
Help in java and mysql connector

Hi Injesus,

Thanks for ur reply. Will u please tell me the file name which u gave in ur reply.It'll be more helpful for me and with the grant option, i had granted all privileges to tomcat user. But still its not working.Please advice me more on this.

Thanks a lot.
 
Old 10-29-2005, 05:31 AM   #4
deepanvenkatesh
LQ Newbie
 
Registered: Oct 2005
Location: Bangalore
Posts: 19

Original Poster
Rep: Reputation: 0
Help in java and mysql connecting

Hi there,

I checked the testmysql.java file and nothing wrong in it.The samwprogram is working fine for other server.

I think there may be some permission problem or some jar files may miss from their path.I'll check it now and if u have any ideas help me on this.

Thanks a lot.
 
Old 10-29-2005, 12:34 PM   #5
InJesus
Member
 
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33

Rep: Reputation: 15
More Ideas

Well Deep,

You could Import java.sql and use the driver you're using with the code I provided or you could use java.lang.* I think thats where the MM MySQL driver is. You can name the file whichever you'd like. If it is MySQL perhaps you didn't have permission to create a user, were you logged into mysql as root? if thats not It and you were missing a driver you'd get an error compiling the code, so I would worry about that. Try the code I wrote, Name it whatever you like.

Good Luck
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql and mysql java connector, connection refused arubin Slackware 3 03-29-2008 01:41 AM
Connecting to MySQL using Java Rodotheos Programming 2 06-17-2005 07:23 AM
java app hanging when connecting to mysql blank_slate Programming 3 11-15-2004 09:52 AM
Connecting to MySQL ksalopek Linux - Software 5 06-11-2004 09:07 AM
Mysql is not connecting from servlet.But connecting from java, help pls Harish_f Linux - General 0 05-08-2002 03:21 AM


All times are GMT -5. The time now is 04:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration