Setting up JDBC Connection
First, you need to download the MYSQL connector for Java.
And setting up a JDBC connection in Eclipse is much easier. You can try this,
Right-click on your Java project in the package explorer in Eclipse workspace and go to Properties Go down the list that appears until you find Java Build Path and click Libraries and then click Add External Archives. Find the downloaded jar called mysql-connector-java-versionnumber.jar and choose it. This should import your jar file to Eclipse
And finally, you can code. Here is a sample Java JDBC connection,
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
class MySQLConnection {
public static void main(String[] args) {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/some_db";
String username = "some_username":
String password = "some_password";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, username, password);
System.out.println("Connected!");
} catch (SQLException ex) {
throw new Error("Error ", ex);
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
}
Want to learn Java? Check out the Java certification from Intellipaat.