TechArena Community

TechArena Community (
-   Software Development (
-   -   Mysql connection with java (

Xena 24-04-2009 03:04 PM

Mysql connection with java
Guys, looking for the web I found the classes already made for connection to a DB from java. My code is as follows:

NameDB String = "DB_Example";
String username = "Newuser123";
PwdUtente String = "XXXXXXX";
Calendar calendar = new IndianCalendar ();
int hours = calendar.get (Calendar.HOUR_OF_DAY);
int minutes = calendar.get (Calendar.MINUTE);
int seconds = calendar.get (Calendar.SECOND);

try (

Class.forName ( "com.mysql.jdbc.Driver"). NewInstance ();
Connection db = DriverManager.getConnection ( "jdbc: mysql: / / localhost /" + + nomeDB? User = "+ username +" & password = "+ pwdUtente);
/ / Sample query INSERT
String SQL = "INSERT INTO access (time) VALUES (?)";

/ / Create the statement
PreparedStatement SQLPreparedStatement = db.prepareStatement (SQL);

/ * Set the parameters (values to be replaced with question marks).
* The numbering starts at 1.
* /
SQLPreparedStatement.setString (1, "" + hours +":"+ minutes +":"+ seconds);

/ / Execute the statement
SQLPreparedStatement.executeUpdate ();

/ / Close the statement
SQLPreparedStatement.close ();
db.Close ();

Now if I try to connect to localhost on my pc all functions correctly. If I try to connect to DB on-line mysql, gives me access denied:

java.sql.SQLException: Access denied for user 'DB_Example' @ 'localhost' (using password: YES)

I checked all the data is correct, where I am wrong?

Thank you very much

Astro121 24-04-2009 03:06 PM

Re: Mysql connection with java

Connection db = DriverManager.getConnection ( "jdbc: mysql: / / localhost /" + + nomeDB? User = "+ username +" & password = "+ pwdUtente);
you must replace localhost with the address of the db.

Ackley 24-04-2009 03:16 PM

Re: Mysql connection with java
1.You can check it is installed successfully or not from START-> COntrol Panel->Administrative Tools ->Data Sources (ODBC).
It will open ODBC Datasource Administrator. Click ADD. You will find MYSQL ODBC 3.51 or 5.1 Driver.
If you find it then it is installed correctly and ready for use.

2.If you istall mysql successfully then craete a table is the database (Use your own database).


3.then Create a tbale:
CREATE TABLE my_test_table(id INT(3), name VARCHAR(20));

Then Insert some values in the db:
INSERT INTO my_test(id,name) VALUES(1,’udaybhanu’);
INSERT INTO my_test(id,name) VALUES(2,’qwer’);
INSERT INTO my_test(id,name) VALUES(3,’asdf’);
INSERT INTO my_test(id,name) VALUES(4, ‘zxcv’);

4.Then if you have IIS Installed on the server then create a virtual directory to run ASP ( If you have it already then leave this step.)

5. Then create a file named connect-mysql.asp and put the contents written below.

All times are GMT +5.5. The time now is 07:43 AM.