Results 1 to 6 of 6

Thread: Class.forName

  1. #1
    f22raptor is offline Member
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default Class.forName

    Hi all

    I tried to run a sample code mentioned below and i get compilation error: cannot find symbol
    symbol: method forName(java.lang.String)
    location: class Class
    Class.forName(driver);

    This is the below code:


    //STEP 1. Import required packages
    import java.sql.*;

    public class FirstExample {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/EMP";

    // Database credentials
    static final String USER = "username";
    static final String PASS = "password";

    public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try{
    //STEP 2: Register JDBC driver
    Class.forName("com.mysql.jdbc.Driver");

    //STEP 3: Open a connection
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);

    //STEP 4: Execute a query
    System.out.println("Creating statement...");
    stmt = conn.createStatement();
    String sql;
    sql = "SELECT id, first, last, age FROM Employees";
    ResultSet rs = stmt.executeQuery(sql);

    //STEP 5: Extract data from result set
    while(rs.next()){
    //Retrieve by column name
    int id = rs.getInt("id");
    int age = rs.getInt("age");
    String first = rs.getString("first");
    String last = rs.getString("last");

    //Display values
    System.out.print("ID: " + id);
    System.out.print(", Age: " + age);
    System.out.print(", First: " + first);
    System.out.println(", Last: " + last);
    }
    //STEP 6: Clean-up environment
    rs.close();
    stmt.close();
    conn.close();
    }catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
    }catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
    }finally{
    //finally block used to close resources
    try{
    if(stmt!=null)
    stmt.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
    if(conn!=null)
    conn.close();
    }catch(SQLException se){
    se.printStackTrace();
    }//end finally try
    }//end try
    System.out.println("Goodbye!");
    }//end main
    }//end FirstExample

    Also i downloaded the Mysqlconnector.jar file and copied it to my jdk/lib directory and set the class path the same.

    Can any 1 please help me with that please??

    Thank you.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Class.forName

    As you seem to have a habit of never returning to the threads you start, I'm not particularly interested in making any suggestions here.

    db

  3. #3
    f22raptor is offline Member
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default Re: Class.forName

    Sorry for that. As am engaged in my regular activity i find difficult to revert back on my own threads.

    But as for the above problem i found the mistake i did!! Am so dumb.

    The thing is that i had saved the program in my project folder which already had a class by name Class and that was what creating the problem. Now i moved the file onto the different folder and it compiles fine but this time am thrown runtime error which says driver not found!! Any clue about that???

    Thank you

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Class.forName

    ... already had a class by name Class
    That was rather obvious, and is why one should avoid reusing class names that are present in the standard JDK, and especially never reuse names from the java.lang package.

    db

  5. #5
    f22raptor is offline Member
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default Re: Class.forName

    yes but what about no suitable driver not found???

  6. #6
    f22raptor is offline Member
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default Re: Class.forName

    And also when i try the same code in netbeans it throws the same error but the highlight is on the "con = DriverManager.getConnection("url", "username", "password");"statement. Is that the correct way of opening the correction?? Or is the way of setting my class path wrong ??

Similar Threads

  1. Class.forName()
    By jomypgeorge in forum New To Java
    Replies: 6
    Last Post: 05-02-2011, 02:56 PM
  2. Class.forName and casting
    By martinmarinov in forum Advanced Java
    Replies: 16
    Last Post: 06-09-2010, 02:42 PM
  3. Class.forName Exception
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-21-2009, 06:08 AM
  4. about Class.forName
    By angus203 in forum New To Java
    Replies: 0
    Last Post: 11-25-2007, 04:47 AM
  5. question about Class.forName()
    By oregon in forum JDBC
    Replies: 4
    Last Post: 08-01-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •