473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

unable to connect to mysql server

rhitam30111985
112 100+
hi all .. java/jdbc newbie here ... i cant seem to perform the most basic step for database connectivity using java . here is my code :

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.sql.*;
  3.  
  4.    public class connect
  5.    {
  6.        public static void main (String[] args)
  7.        {
  8.            Connection conn = null;
  9.  
  10.            try
  11.            {
  12.                String userName = "myname";
  13.                String password = "mypassword";
  14.                String url = "jdbc:mysql:localhost";
  15.                Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  16.                conn = DriverManager.getConnection (url, userName, password);
  17.                System.out.println ("Database connection established");
  18.            }
  19.            catch (Exception e)
  20.            {
  21.                System.err.println ("Cannot connect to database server");
  22.            }
  23.            finally
  24.            {
  25.                if (conn != null)
  26.                {
  27.                    try
  28.                    {
  29.                        conn.close ();
  30.                        System.out.println ("Database connection terminated");
  31.                    }
  32.                    catch (Exception e) { /* ignore close errors */ }
  33.                }
  34.            }
  35.        }
  36.    }
  37.  
  38.  
when i am connecting to the same server using some other programming language such as python it is working perfectly fine .
what i cant figure out is what to assign to the "url" variable.. in other words .. how shall i determine the url of the company mysql server ? every time i run this code i get the same error "Cannot connect to database server".

I am using debian linux OS

any help appreciated,
regards
rhitam.
Jan 11 '08 #1
10 3375
r035198x
13,262 8TB
Replace
Expand|Select|Wrap|Line Numbers
  1. System.err.println ("Cannot connect to database server");
on line 20 in your code with

Expand|Select|Wrap|Line Numbers
  1. e.printStackTrace()
That way you can see more information about the exception that is being thrown.
Jan 11 '08 #2
rhitam30111985
112 100+
hi r035198x

i did as u said .. got the following errors :

Expand|Select|Wrap|Line Numbers
  1. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
  2.         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
  3.         at java.security.AccessController.doPrivileged(Native Method)
  4.         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
  5.         at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
  6.         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
  7.         at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
  8.         at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
  9.         at java.lang.Class.forName0(Native Method)
  10.         at java.lang.Class.forName(Class.java:169)
  11.         at connect.main(connect.java:15)
  12.  
cant figure what it means
Jan 11 '08 #3
r035198x
13,262 8TB
hi r035198x

i did as u said .. got the following errors :

Expand|Select|Wrap|Line Numbers
  1. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
  2.         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
  3.         at java.security.AccessController.doPrivileged(Native Method)
  4.         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
  5.         at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
  6.         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
  7.         at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
  8.         at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
  9.         at java.lang.Class.forName0(Native Method)
  10.         at java.lang.Class.forName(Class.java:169)
  11.         at connect.main(connect.java:15)
  12.  
cant figure what it means
That means the MySQL driver classes cannot be found by your program. You should add the MySQL connector jar file's path to your program's classpath.
Jan 11 '08 #4
rhitam30111985
112 100+
uh.. how exactly do i do that ?
Jan 11 '08 #5
r035198x
13,262 8TB
uh.. how exactly do i do that ?
Do you have the MySQL driver .jar file with you?
Jan 11 '08 #6
rhitam30111985
112 100+
i have already installed the jdbc driver for mysql.. namely

libmysql-java

but when i search for it using the following command :

whereis libmysql-java

it shows blank
Jan 11 '08 #7
r035198x
13,262 8TB
i have already installed the jdbc driver for mysql.. namely

libmysql-java

but when i search for it using the following command :

whereis libmysql-java

it shows blank
Download the connector .jar from here and add it to your classpath.
Jan 11 '08 #8
rhitam30111985
112 100+
ok i downloaded and extracted the following file :

mysql-connector-java-3.0.17-ga.tar.gz

now by adding it to classpath do u mean just copy and paste it there? in that case how do i determine what is the classpath?

thanks
Jan 11 '08 #9
ajos
283 100+
ok i downloaded and extracted the following file :

mysql-connector-java-3.0.17-ga.tar.gz

now by adding it to classpath do u mean just copy and paste it there? in that case how do i determine what is the classpath?

thanks
Now that you have got the mysql-connector-java-3.0.17-ga.tar.gz link just unzip the file, it contains a mysql-connector-java-3.0.17--bin which is a jar file, put the copy of the jar file in the lib folder of the application and include the path of the jar file in the class path. For more information read the mysql connector docs, it has everything that you need.
regards,
ajos
Jan 11 '08 #10
rhitam30111985
112 100+
Hi all , sorry for making this topic active again but still facing problems ...

to add the jar file to classpath i did this :

downloaded the jar file and unipped it and placed in the following folder :

/home/rhitam/java/myprograms/

now to add to class path .. i have to first change the accessibilty of this jar file ... .

chmod +x /home/rhitam/java/myprograms/mysql-connector-java-3.0.17-ga-bin.jar

this worked ..
then .. i did this ...
set CLASSPATH=.;/home/rhitabrata/java/myprograms/mysql-connector-java-3.0.17-ga-bin.jar

it gave the error:


-bash: /home/rhitabrata/java/myprograms/mysql- connector-java-3.0.17-ga-bin.jar: cannot execute binary file


the code is also located in the same location as the jar file ... ( /home/rhitam/java/myprograms/)

what to do ?
Jan 23 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sridhar Reddy | last post by:
Hi, Where can i get the download mysql.sock file? I am unable to connect to local server because i dont have this file? thanx in advance Sridhar
2
by: Mondal | last post by:
Hi, I am using MySQL 5.0 beta and Active Python 2.4. I have the correct version of MySQLdb installed. The problem is that I can't connect to MySQL through the Active Python interactive window. ...
3
by: pc | last post by:
hi everyone, I am trying to connect to a new mysql installation. I have given root permission to connect to all databases from anywhere; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY...
1
by: Nospam | last post by:
on trying to change the password it says this: C:\PROGRA~1\MYSQL\MYSQLS~1.0\BIN\MYSQLA~1.EXE: connect to server at 'localhost' failed error: 'Can't connect to MySQL server on 'localhost'...
23
cyberking
by: cyberking | last post by:
Hi. I have just started learning PHP along with MySQL. I am trying to connect to the MySQL DB server but unable to. I have been tryin. Please help. Its urgent cos I shall soon have to work on...
1
by: iceboy | last post by:
Hello to all,I have a problem to connect PHP & MySQL together. I have created a form that gets the username & password from admin and connect him to database.the part of PHP script that connect to...
1
by: ynyus75 | last post by:
Hey! I am unable to connect some of the services that I host from my laptop at home, such as SMTP and MySQL on port numbers 25 and 3306 respectively. But I am able to some services such as...
2
by: wence | last post by:
I have installed MySQL Community server and PHP 5.2.2( installed manually, no installer) on my pc I configured php with IIS (version 5) properly. If I open a php file in a webbrowser(internet...
8
by: malaysiauser | last post by:
Im new user of PHP and MySQL. I tried to use a script package for my web. I'd created a database. And my connection scripts are as follow; <?php $mysql_host="localhost"; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.