How to connect Java Eclipse to MySQL Server?

JDBC connectivity to MySQL Server

  1. Download MySQL JDBC Driver.
  2. Upload .rar file of JDBC Driver to the Eclipse.
  3. Copy and paste java code in eclipse.

Steps:

1. Download MySQL JDBC Driver from http://dev.mysql.com/downloads/connector/j/
2.  After downloading the zip file, extract it and add this .rar file to lib folder of eclipse project.
Learn how to add .rar file to the eclipse folder by following the steps(1-5) from this post.

3. Make a class file and paste the following code.
import java.sql.*;
//import the all valuable stuffs frm SQL
public class driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
//Get connection
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/new“,”root“,”root“);
// here , new is the name of database that i created from MySQL Workbench, first root is my username of sql server and second root is my password. 
// make statement
Statement st=con.createStatement();
//execute sql query
ResultSet rs=st.executeQuery(“select * from new_table”);
// new_table is the name of my table that i have created from the MySQL Server Workbench.
//process result set
while(rs.next())
{
System.out.println(rs.getString(“name“));
// name is the column name of my table.
}
}catch( Exception exc){
exc.printStackTrace();
}
}
}
More Tutorials

2 thoughts on “How to connect Java Eclipse to MySQL Server?”

Leave a Comment