Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JDBC help

  • 03-04-2002 6:49pm
    #1
    Closed Accounts Posts: 43


    Hello all. I'm trying to connect to a SQL database in Linux using the following. The code compiles fine, but when I try to run it I get the following error:

    Exception in thread "main" java.lang.NoClassDefFoundError: mess2/class

    *any* help at all would be greatly apreciated.
    ======================================================================
    import java.sql.*;

    public class mess2 {

    public static void main(String[] args) throws Exception {

    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    Connection Conn = null;

    String url = "jdbc:mysql://csserver.ucd.ie";
    String userName = "xxxxxx";
    String pass = "xxxxx";
    Conn = DriverManager.getConnection(url, userName, pass);
    Statement stmt = Conn.createStatement();
    ResultSet RS = stmt.executeQuery("SELECT * FROM film WHERE name='American-Pie'");

    String s = RS.getString("name");
    System.out.println("this is the first film in the database " + s);

    // Clean up after ourselves
    RS.close();
    stmt.close();
    Conn.close();
    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 1,509 ✭✭✭viking


    to run this code:

    C:\> java mess2

    Dont type "java mess2.class" because then java thinks that you are using a package called mess2 which contains the class "class".

    Try it, should work...

    viking


  • Closed Accounts Posts: 43 Kid Icarus


    Nope - won't work. Besides the fact that I'm working from Linux, I have already tried the equivalent form of that in Linux.


  • Registered Users, Registered Users 2 Posts: 1,509 ✭✭✭viking


    oops, didn't see you were running linux.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Is the current directory in your classpath?
    try 'java ./mess2'
    or put the '.' into your classpath.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    If you try and compile a program and get an error such as

    java.lang.NoClassDefFoundError

    It's usually a problem with your classpath. This means that the code you are trying to compile can either not be found itself, or else something that it tries to import cannot be found.

    As Enygma said in the last post, to append '.' to your classpath, this is true when the compiler cannot find the code you wrote. What you have to do now is:

    set CLASSPATH = %CLASSPATH%;.

    The above line may vary in a linux shell, but that's what it looks like for a DOS command prompt.

    Also if you want to see the value of your classpath variable at any time just type:

    echo %CLASSPATH%

    from the DOS command prompt.

    ;-phobos-)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,660 ✭✭✭Blitzkrieger


    This might not be the problem, or even right but :
    Originally posted by Kid Icarus
    throws Exception

    What kind of exception?
    Originally posted by Kid Icarus


    String url = "jdbc:mysql://csserver.ucd.ie";

    Every example I've ever seen the url was "jdbc:odbc:dbname" but that's not to say it's wrong.

    Also - you probably have to catch exceptions. Accessing the db you usually have to do a try-catch. Something like :

    try
    {
    //load database driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //establish a connection
    Connection con = DriverManager.getConnection(url, username, password);
    //create a statement
    Statement stmt = con.createStatement();
    String query = (")");

    //execute statement
    ResultSet rs = stmt.executeQuery(query);
    //output results from results set
    while (rs.next())
    {

    model = rs.getString("model");
    }//end while
    }
    catch(ClassNotFoundException cnfex)
    {
    System.err.println("Failed to load JDBC/ODBC driver.");
    cnfex.printStackTrace();

    }
    catch(SQLException sqlex)
    {
    System.err.println("Unable to connect.");
    sqlex.printStackTrace();

    }


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    It's ok to say throws Exception, just better to say throws NumberFormatException or whatever.

    The URL to the DB is fine too.

    You're right about the whole thing being wrapped in a try catch, and if you do catch and Exception, throw one like you said you would :)


Advertisement