Results 1 to 6 of 6
Thread: Class.forName
- 09-18-2011, 07:11 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
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.
- 09-18-2011, 10:07 AM #2
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
- 09-18-2011, 11:12 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
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
- 09-18-2011, 11:41 AM #4
Re: Class.forName
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.... already had a class by name Class
db
- 09-18-2011, 02:12 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Re: Class.forName
yes but what about no suitable driver not found???
- 09-18-2011, 02:26 PM #6
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Class.forName()
By jomypgeorge in forum New To JavaReplies: 6Last Post: 05-02-2011, 02:56 PM -
Class.forName and casting
By martinmarinov in forum Advanced JavaReplies: 16Last Post: 06-09-2010, 02:42 PM -
Class.forName Exception
By Moncleared in forum Advanced JavaReplies: 5Last Post: 02-21-2009, 06:08 AM -
about Class.forName
By angus203 in forum New To JavaReplies: 0Last Post: 11-25-2007, 04:47 AM -
question about Class.forName()
By oregon in forum JDBCReplies: 4Last Post: 08-01-2007, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks