LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sorry if thread is in wrong area .. (https://www.linuxquestions.org/questions/linux-newbie-8/sorry-if-thread-is-in-wrong-area-704743/)

vibinlakshman 02-15-2009 12:26 AM

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

paulsm4 02-15-2009 12:37 AM

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)?

vibinlakshman 02-15-2009 12:41 AM

Here is the ouput
 
Exception: com.mysql.jdbc.Driver
This is what i get as output

paulsm4 02-15-2009 01:07 PM

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

vibinlakshman 02-15-2009 08:10 PM

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 ?

paulsm4 02-15-2009 08:58 PM

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

vibinlakshman 02-16-2009 01:14 AM

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


All times are GMT -5. The time now is 08:53 AM.