Tag Archives: insert

In this tutorial , i will teach you about a database connectivity with mysql database using java programming language.

As java is a object oriented programming language so the best approach to follow is :

1. Make a individual package for a database.

1.1 In this package create a class for database connectivity.

2. Make a individual packages for each of tables in a database.

2.1 In each of package make a bean class of a particular table.

2.2 Make a class for a task which you want to do. (e.g Selection , insertion etc ).

The following code is for a database connectivity with mysql refer to step 1 and 1.1

 DatabaseConnection.java

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class DatabaseConnection {

public static Connection con;

public static void init() throws Exception{
String JDBC_DRIVER   = "com.mysql.jdbc.Driver";
String JDBC_URL      = "jdbc:mysql://localhost:3306/test";
String JDBC_USER     = "root";
String JDBC_PASSWORD = "";

Class.forName(JDBC_DRIVER);
con = (Connection)
DriverManager.getConnection(JDBC_URL,JDBC_USER,JDBC_PASSWORD);
System.out.println("Driver Loaded Sucessfully");

}//end init
}

now i want to fetch record so i follow the step no 2.

for example create EmployeeBean.java class. In a bean class we have only setters and getter of a properties.

package employee;
public class EmployeeBean {
private int emp_id;
private String emp_name;
private String emp_fname;
private String emp_surname;

public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public String getEmp_fname() {
return emp_fname;
}
public void setEmp_fname(String emp_fname) {
this.emp_fname = emp_fname;
}
public String getEmp_surname() {
return emp_surname;
}
public void setEmp_surname(String emp_surname) {
this.emp_surname = emp_surname;
}

@Override
public String toString(){
return emp_name;
}

}

use following where you want to perform database operations.

class EmpData{

public  Vector<EmployeeBean> getAllEmployees() throws SQLException{
String query = "SELECT emp_id, emp_name, emp_fname, emp_surname FROM employee";

Statement ps = null;
ResultSet rs = null;
Vector<EmployeeBean> vector = new Vector<EmployeeBean>();
try{
ps = (Statement)database.Mysqlstudent.con.createStatement();
rs = ps.executeQuery(query);
while(rs.next()){
EmployeeBean bean = new EmployeeBean();
bean.setEmp_id(rs.getInt("emp_id"));
bean.setEmp_name(rs.getString("emp_name"));
bean.setEmp_fname(rs.getString("emp_fname"));
bean.setEmp_surname(rs.getString("emp_surname"));
vector.add(bean);
}//end while loop
}finally{
if(ps != null) ps.close();
if(rs != null) rs.close();
}

return vector;

}//end getEmployees

}

and use it by creating a object of Empdata where we requires it. Like,

Empdata data = new Empdata();

Vector<EmployeeBean> vect = data.getAllEmployees();

so in vect variable you get all record of employees.