import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mindtree.kalinga.exception.ConnectionfailedException;
public class JdbcConnection {
private static JdbcConnection jdbcConnection;
private static Connection dbConnection;
private JdbcConnection() {
}
public static JdbcConnection getInstance() {
if (jdbcConnection == null)
jdbcConnection = new JdbcConnection();
return jdbcConnection;
}
public static void createConnection() throws ConnectionfailedException {
try {
dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mindtree", "root", "Welcome123");
} catch (SQLException e) {
throw new ConnectionfailedException("Error creating the connection to database", e);
}
System.out.println("Connection to db Established!");
}
public static Connection getConnection() {
return dbConnection;
}
public static void closeConnection() throws ConnectionfailedException {
try {
dbConnection.close();
} catch (SQLException e) {
throw new ConnectionfailedException("connection not closed properly", e);
}
}
}
--------------------------------------
Main
--------------------------------------
package com.mindtree.kalinga.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.mindtree.kalinga.entity.Mindtree;
import com.mindtree.kalinga.exception.ConnectionfailedException;
import com.mindtree.kalinga.service.Mindtreeservice;
import com.mindtree.kalinga.serviceimp.Mindtreeserviceimp;
import com.mindtree.kalinga.utility.JdbcConnection;
public class MindtreeMain {
static Scanner in = new Scanner(System.in);
static Mindtreeservice ms = new Mindtreeserviceimp();
public static void main(String[] args) {
try {
JdbcConnection.createConnection();
} catch (ConnectionfailedException e) {
System.out.println("Not able to connect to database! Terminating application!");
e.printStackTrace();
}
MindtreeMain mm = new MindtreeMain();
int i = 1;
while (i == 1) {
System.out.println("1. to enter the detail\n" + "2. fetch the detail\n" + "3. to exit");
int key = in.nextInt();
switch (key) {
case 1:
Mindtree m = mm.createminds();
break;
case 2:
List<Mindtree> ml = new ArrayList<>();
mm.display(ml);
break;
case 3:
i = 0;
break;
default:
break;
}
}
}
private Mindtree createminds() {
// TODO Auto-generated method stub
System.out.println("enter the id of mind");
int id = in.nextInt();
in.nextLine();
System.out.println("enter the name of mind");
String name = in.nextLine();
System.out.println("ente the address of minds");
String address = in.nextLine();
Mindtree m = new Mindtree(id, name, address);
return m;
}
private void display(List<Mindtree> mind) {
for (Mindtree i : mind) {
System.out.println(i.getMid());
System.out.println(i.getName());
System.out.println(i.getAddress());
}
}
}
-------------------------------------------------
service
-------------------------------------------------
import java.util.List;
import com.mindtree.kalinga.entity.Mindtree;
public interface Mindtreeservice {
public void insertmind(Mindtree m);
public List<Mindtree> getAllminds();
}
---------------------------------------------
serviceimp
---------------------------------------------
package com.mindtree.kalinga.serviceimp;
import java.util.List;
import com.mindtree.kalinga.dao.Mindtreedao;
import com.mindtree.kalinga.daoimp.Mindtreedaoimp;
import com.mindtree.kalinga.entity.Mindtree;
import com.mindtree.kalinga.service.Mindtreeservice;
public class Mindtreeserviceimp implements Mindtreeservice {
Mindtreedao md = new Mindtreedaoimp();
@Override
public void insertmind(Mindtree m) {
// TODO Auto-generated method stub
md.insertMindToDb(m);
}
@Override
public List<Mindtree> getAllminds() {
// TODO Auto-generated method stub
return md.getAllMindFromDb();
}
}
--------------------------------------------
dao
--------------------------------------------
import java.util.List;
import com.mindtree.kalinga.entity.Mindtree;
public interface Mindtreedao {
public void insertMindToDb(Mindtree m);
public List<Mindtree> getAllMindFromDb();
}
-------------------------------------------
daoimp
-------------------------------------------
package com.mindtree.kalinga.daoimp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mindtree.kalinga.dao.Mindtreedao;
import com.mindtree.kalinga.entity.Mindtree;
import com.mindtree.kalinga.utility.JdbcConnection;
import com.mysql.jdbc.Statement;
public class Mindtreedaoimp implements Mindtreedao {
@Override
public void insertMindToDb(Mindtree m) {
Connection con = JdbcConnection.getConnection();
String query = "insert into mindtree values(?,?,?);";
PreparedStatement ps = null;
try {
ps = con.prepareStatement(query);
ps.setInt(1, m.getMid());
ps.setString(2, m.getName());
ps.setString(3, m.getAddress());
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
@Override
public List<Mindtree> getAllMindFromDb() {
// TODO Auto-generated method stub
List<Mindtree> mtl = new ArrayList<Mindtree>();
Connection con = JdbcConnection.getConnection();
String query = "select * from mindtree;";
Statement st = null;
ResultSet rs = null;
try {
st = (Statement) con.createStatement();
rs = st.executeQuery(query);
while (rs.next()) {
Mindtree m = new Mindtree(rs.getInt(1), rs.getString(2), rs.getString(3));
mtl.add(m);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mtl;
}
}
-----------------------------------------------
exception
package com.mindtree.kalinga.exception;
public class ConnectionfailedException extends Exception {
public ConnectionfailedException() {
super();
// TODO Auto-generated constructor stub
}
public ConnectionfailedException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
public ConnectionfailedException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public ConnectionfailedException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public ConnectionfailedException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
}
-----------------------------
entity
-----------------------------
package com.mindtree.kalinga.entity;
public class Mindtree {
int Mid;
String name;
String address;
public Mindtree(int mid, String name, String address)
{
Mid = mid;
this.name = name;
this.address = address;
}
public int getMid() {
return Mid;
}
public void setMid(int mid) {
Mid = mid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}