Reputation: 31
How can i connect mysql db with eclipse, can someone please help me.what version of eclipse,JRE i need and how to fetch or what IDE is easy to connect mysql such as netbeans or pycharm if python is easy.
Upvotes: 2
Views: 37362
Reputation: 2436
Which version?
You can use any version. But the latest is always better.
Which JRE?
If you are developing applications using Java you need to install JDK. It will install the JRE. And you need to add JDK bin path to your System Variables. And here and here are examples that showing how to do that.
How to 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 the 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 file called mysql-connector-java-version number.jar
and choose it. This should import your jar file to Eclipse!
Finally,
And now you can code. 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());
}
}
}
}
Hope this helps!
Upvotes: 3
Reputation: 1
You can either use eclipse neon or eclipse IDE for Java Developers.Then you have to attach Mysql connection drivers to your program.Then you can write this code in your program.
Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
test is your db name you can write yours
Upvotes: 0
Reputation: 31
You will need java mysql connector jar and add it to java build path. Refer this : https://ibytecode.com/blog/jdbc-mysql-connection-tutorial/
Upvotes: 0