I have an issue in which I can't make MySQL request through java because the driver cannot be initialized as it can't be found. There is question already asked that attempted at solving a similar issue but after trying many solution presented nothing made it.
The issue comes from the line Class.forName(JDBC_DRIVER);
where an attempt to initialize com.mysql.jdbc.Driver
is made. I have a ClassNotFoundException
there. According to answers to question already asked, I should simply download JAR file MySQL Connector driver, and add it to my project classpath by importing it: I created a folder named lib in my classpath and added the mysqlc-connector.x.x.x.jar file to it and refreshed my project.
Here is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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/ChatWorld";
// Database credentials
static final String USER = "root";
static final String PASS = "********";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName(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 * FROM account";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
String username = rs.getString("username");
String email = rs.getString("email");
String birthdate = rs.getString("birthdate");
//Display values
System.out.print("ID: " + id);
System.out.print(", Username: " + username);
System.out.print(", Email: " + email);
System.out.println(", Birthdate: " + birthdate);
}
//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
I tried many searches on Internet, and all sites I went on all recommended on importing MySQL connector library and their solution seem to be ruling out the issue according to other users, except in my case as it looks like, but I don't know what is going wrong for me.
I am using Eclipse as an IDE.
Let's just point out I am new to java, I am a self-learner so I have not learned every subtle point of java already and I might have missed other important point in my configuration.
Thanks for your help in advance to anyone.
P.S.: I hesitated to post a comment to some answerers or ask a new question, but thought my issue could be more specific.