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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-28-2005, 02:13 PM
|
#1
|
LQ Newbie
Registered: Oct 2005
Location: Bangalore
Posts: 19
Rep:
|
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.
|
|
|
10-28-2005, 03:24 PM
|
#2
|
Member
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33
Rep:
|
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
}
|
|
|
10-29-2005, 05:17 AM
|
#3
|
LQ Newbie
Registered: Oct 2005
Location: Bangalore
Posts: 19
Original Poster
Rep:
|
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.
|
|
|
10-29-2005, 05:31 AM
|
#4
|
LQ Newbie
Registered: Oct 2005
Location: Bangalore
Posts: 19
Original Poster
Rep:
|
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.
|
|
|
10-29-2005, 12:34 PM
|
#5
|
Member
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 06:03 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|