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. |
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 } |
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. |
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. |
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:01 AM. |