How to connect to mysql database in java using eclipse ?

Connecting java to mysql using jdbc in eclipse

In this post, we are going to see how to connect to the MySQL database in javusing jdbc driver.We tried to keep it very simple so that you can understand it better.We have created a simple entity class Employee.java and will fetch data from employee table.

Tools Required:

  • Java IDE (Eclipse)
  • Mysql
Jar:
  • jar: mysql-connector-java-5.1.22-bin
Steps :

1- Create a new  java project.

2- To connect to the database we need database driver class com.mysql.jdbc.Driver which is available in mysql-connector-java-5.1.22-bin.jar .So add this jar in the build path.

3-Create an Entity Class and Data access(A java class which will interact with the database) class .

Employee.java



public class Employee {
private int empId;
private String empName;
private double empSal;
private String desig;
/**
* @return the empId
*/
public int getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(int empId) {
this.empId = empId;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empSal
*/
public double getEmpSal() {
return empSal;
}
/**
* @param empSal the empSal to set
*/
public void setEmpSal(double empSal) {
this.empSal = empSal;
}
/**
* @return the desig
*/
public String getDesig() {
return desig;
}
/**
* @param desig the desig to set
*/
public void setDesig(String desig) {
this.desig = desig;
}

}


TestDao.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TestDao {
//Method to fetch list from database table 

public List<Emp> getEmployees(){
List<Emp> lst=new ArrayList<Emp>();
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
Emp emp=null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mindtree";
conn=getConnection(url,"root","Welcome123");
st=conn.prepareStatement("select * from emp order by emp_sal desc;");
   rs=st.executeQuery();
while(rs.next()){
emp=new Emp();
emp.setEmpId(rs.getInt("emp_id"));
emp.setEmpName(rs.getString("emp_name"));
emp.setEmpSal(rs.getDouble("emp_sal"));
emp.setDesig(rs.getString("desig"));
lst.add(emp);
}
} catch(ClassNotFoundException e){
System.out.println(e);
}catch(SQLException e){
System.out.println(e);
}finally{
closeConn(conn);
}
return lst;
}

//Method to close the connection
private static void closeConn(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
//Method to establish connection on passing url,user and password of database
public static Connection getConnection(String url,String user,String pass){
Connection conn =null;
try {
conn=DriverManager.getConnection(url, user, pass);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}


Code Flow:


  • In DAO class TestDao.java first, we are loading the driver class.
  • We are getting the connection object from DriverManager by calling getConnection method and passing database credentials and URL to this method.
  • On the connection object, we are calling prepareStatement to run the query.We also use statement instead of prepareStatement
  • Execute query and get the resultset,





4.Finally, we need to create the main method to run the application.

PrintList.java

import java.util.List;

public class PrintList {
public static void main(String[] args)throws Exception{
TestDao dao=new TestDao();
List<Employee> lst=dao.getEmployees();
for(Employee emp:lst){

System.out.println(emp.getEmpId()+" "+emp.getEmpName()+" "+emp.getDesig()+" "+emp.getEmpSal());

}
}

}


Hope you like this basic tutorial.

Give you suggestion in comments.If you like the post Share with your friends on social network.











How to connect to mysql database in java using eclipse ?  How to connect to mysql database in java using eclipse ? Reviewed by JavaInstance on 7:18:00 AM Rating: 5

No comments:

Powered by Blogger.