LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-15-2009, 12:26 AM   #1
vibinlakshman
Member
 
Registered: Dec 2008
Location: Kerala, India
Distribution: Ubuntu 11.10
Posts: 334

Rep: Reputation: 33
Cool Sorry if thread is in wrong area ..


I just had a doubt , if this is not area to ask , i would apologise for that , and kindly remove this thread
I had written a simple prgrm in java connecting with mysql
i had downloaded the mysqlconnector for java , latest version frm mysql.com
I'm not able to get the output of program , just shows driver not loaded
Can anyone suggest how to overcome this .. please
 
Old 02-15-2009, 12:37 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I cut/pasted everything but the package from this link:

http://www.stardeveloper.com/article...3090401&page=1

Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class HelloJdbc {

  public static void main(String args[]) {
    Connection con = null;

    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql:///test",
        "root", "secret");

      if(!con.isClosed())
        System.out.println("Successfully connected to " +
          "MySQL server using TCP/IP...");

    } catch(Exception e) {
      System.err.println("Exception: " + e.getMessage());
    } finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}
    }
  }
}
Q: Can you compile and run this snippet (adapted for your particular mySQL install)?
 
Old 02-15-2009, 12:41 AM   #3
vibinlakshman
Member
 
Registered: Dec 2008
Location: Kerala, India
Distribution: Ubuntu 11.10
Posts: 334

Original Poster
Rep: Reputation: 33
Cool Here is the ouput

Exception: com.mysql.jdbc.Driver
This is what i get as output
 
Old 02-15-2009, 01:07 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Sorry -

a) the example's error checking wasn't quite clear enough. The new version below should work better.

b) you usually explicitly specify your JDBC .jar file in your "classpath" argument.

c) you definitely need to make sure you can access mysql itself (e.g. from the command line).

Here is a revised example. I tried it under SusE Linux with Sun Java and MySQL. It worked for me; I hope it works for you, too.

Code:
/*
 * Hello JDBC Sample:
 *
 * EXAMPLE USAGE:
 * 1. Find mysql JDBC driver:
 *    -ls -l /usr/share/java/*conn*.jar =>
 * -rw-r--r--  1 root root 391437 Mar 19  2005 /usr/share/java/mysql-connector-java-3.1.6.jar
 * lrwxrwxrwx  1 root root     30 May 24  2006 /usr/share/java/mysql-connector-java.jar -> mysql-connector-java-3.1.6.jar
 *
 * 2. Verify Mysql connectivity:
 *    mysql -uMYUSERNAME -pMYPASSWORD test =>
 * Welcome to the MySQL monitor.  Commands end with ; or \g.
 * ...
 *    mysql> describe test; =>
 * +-------+------------------+------+-----+---------+----------------+
 * | Field | Type             | Null | Key | Default | Extra          |
 * +-------+------------------+------+-----+---------+----------------+
 * | recID | int(10) unsigned |      | PRI | NULL    | auto_increment |
 * | fname | varchar(20)      |      | MUL |         |                |
 * | lname | varchar(20)      |      |     |         |                |
 * +-------+------------------+------+-----+---------+----------------+
 * +-------+------------------+------+-----+---------+----------------+
 * 3 rows in set (0.00 sec)
 *
 * 3. Compile and run JDBC test:
 *    javac -version HelloJdbc.java =>
 * javac 1.5.0_03
 *    ls -l *class =>
 * -rw-r--r--  1 paulsm users 1367 2009-02-15 13:18 HelloJdbc.class
 *    java -cp ".:/usr/share/java/mysql-connector-java.jar" HelloJdbc =>
 * Successfully connected...
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class HelloJdbc {

  public static void main(String args[]) {

    // Load JDBC driver
    try {
      Class.forName("com.mysql.jdbc.Driver");
    }
    catch (Exception e)
    {
      System.out.println ("Unable to load JDBC driver: " + e.getMessage ());
      return;
    }

    // Establish JDBC connection
    Connection conn = null;
    try {
      conn = DriverManager.getConnection("jdbc:mysql:///test", USER, PASSWORD);
      System.out.println("Successfully connected...");
    }
    catch(Exception e) {
      System.err.println("Other Exception: " + e.getMessage());
    }
    finally {
      try {
        if(conn != null)
          conn.close();
      }
      catch(SQLException e) {}
    }
  }

  private static String USER = "MYUSERNAME";
  private static String PASSWORD = "MYPASSWORD";
}

'Hope that helps .. PSM

Last edited by paulsm4; 02-15-2009 at 08:55 PM.
 
Old 02-15-2009, 08:10 PM   #5
vibinlakshman
Member
 
Registered: Dec 2008
Location: Kerala, India
Distribution: Ubuntu 11.10
Posts: 334

Original Poster
Rep: Reputation: 33
Cool OK ..

1) export CLASSPATH=$CLASSPATH:/home/vibin/Desktop/mysql-connector-java-5.1.7.tar.gz
2) my MySql is working fine


I believe the classpath settings is performing some strange actions, do my settings is correct as u think ?
 
Old 02-15-2009, 08:58 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
1) export CLASSPATH=$CLASSPATH:/home/vibin/Desktop/mysql-connector-java-5.1.7.tar.gz
2) my MySql is working fine
You *cannot* use a ".tar.gz" file in your Java classpath.

Extract it, and use the .jar file. Then you should be golden :-)

Your .. PSM
 
Old 02-16-2009, 01:14 AM   #7
vibinlakshman
Member
 
Registered: Dec 2008
Location: Kerala, India
Distribution: Ubuntu 11.10
Posts: 334

Original Poster
Rep: Reputation: 33
Cool

A bigggggggg thanks dude .. U saved me ... I need to start a project , evrything ok , but just to connect with database left , my design over , need to code it
Again thanks maaan
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
thread not visible in 0-reply-area baikonur LQ Suggestions & Feedback 1 08-04-2006 06:55 AM
desktop area larger than screen resolution (something wrong with X config?) amon Linux - General 5 03-16-2006 01:47 PM
why are my apps using the wrong thread library? dombrowsky Linux - Software 2 02-11-2006 10:09 PM
pthread_cancel(pthread_t thread) wrong on Redhat 9!!!! tclwp Programming 3 01-14-2005 07:08 AM
IRQ? problem or am I looking in the wrong area? LrnLnx Linux - Hardware 1 01-02-2003 12:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration