Connecting Tech Pros Worldwide Forums | Help | Site Map

Mac leapord. NetBeans. Mysql and class path

Newbie
 
Join Date: Oct 2009
Posts: 31
#1: 4 Weeks Ago
Hi All,

The code at the bottom of this message is an example (1 of 3 i have tried) of trying to connect to a localhost mysql database.

It wont, and the exception displayed is the one i have set.

Im fairly convinced its something to do with the driver not being installed.

I have downloaded it and unzipped it and installed it, and got the green success tick.

But it still will not connect.

How do i set a class path?
My only other option is to dual boot windows, which i really dont want to do.

Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2.  
  3.    public class Connect
  4.    {
  5.        public static void main (String[] args)
  6.        {
  7.            Connection conn = null;
  8.  
  9.            try
  10.            {
  11.                String userName = "root";
  12.                String password = "root";
  13.                String url = "jdbc:mysql://localhost:3306/test/";
  14.                Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  15.                conn = DriverManager.getConnection (url, userName, password);
  16.                System.out.println ("Database connection established");
  17.            }
  18.            catch (Exception e)
  19.            {
  20.                System.err.println ("Cannot connect to database server");
  21.            }
  22.            finally
  23.            {
  24.                if (conn != null)
  25.                {
  26.                    try
  27.                    {
  28.                        conn.close ();
  29.                        System.out.println ("Database connection terminated");
  30.                    }
  31.                    catch (Exception e) { /* ignore close errors */ }
  32.                }
  33.            }
  34.        }
  35.    }
  36.  
  37.  

Newbie
 
Join Date: Oct 2009
Posts: 31
#2: 4 Weeks Ago

re: Mac leapord. NetBeans. Mysql and class path


good evening all,

I finally managed to get this script to work.

I had to javac it with the driver in the same directory and it worked fine.

That leads onto my question, how can i avoid that and get it to run in netbeans?
Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#3: 2 Weeks Ago

re: Mac leapord. NetBeans. Mysql and class path


Thanks for letting us know that it worked and what you did to arrive at that conclusion, get a load of this link:

Changing Driver in Java Desktop apllication

Also, fire Google, type in "Netbeans Video Tutorials for Web Applications" and get yourself some coffee call it a night.

You should come out happy:-)
Reply