0% found this document useful (0 votes)
3 views

exjava

The document contains Java code for a menu-driven application that allows users to add, modify, and delete 'Module' entries in a database. It includes classes for database connection, data access object (DAO) operations, and the main menu functionality. The application uses a command-line interface to interact with the user for performing CRUD operations on the 'Module' entity.

Uploaded by

joelgantshua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

exjava

The document contains Java code for a menu-driven application that allows users to add, modify, and delete 'Module' entries in a database. It includes classes for database connection, data access object (DAO) operations, and the main menu functionality. The application uses a command-line interface to interact with the user for performing CRUD operations on the 'Module' entity.

Uploaded by

joelgantshua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

package menu;

import java.util.Scanner;

import dao.ConnectionBD;

import dao.ModuleDAO;

import entites.Module;
public class Menu {
public static Integer choix;

public static ConnectionBD con = new ConnectionBD();


static Scanner clavier=new Scanner(System.in);
public static Module p=new Module();

public static ModuleDAO q=new ModuleDAO();


public static void ajouter()
{
System.out.print("Reference: ");
p.setCode(clavier.nextInt());

System.out.print("Designation: ");
p.setNomM(clavier.next());

String req="insert into Module values ('"+ p.getCode()


+"','"+ p.getNomM() +"')";
con.mAJ(req);}

public static void supprimer()


{System.out.print("Reference à Supprimer: ");
p.setCode(clavier.nextInt());
q.supprimer(p.getCode());

}
public static void modifier()
{

System.out.print("Reference à modifier: ");


p.setCode(clavier.nextInt());
System.out.print("Designation: ");
p.setNomM(clavier.next());

q.miseAjour(p.getCode(), p);
}

public static void main(String[] args) {

con.ouvrir();

System.out.println("Ajout ----------------> 1 : ");


System.out.println("Modifier ------------> 2 : ");
System.out.println("Supprimer -----------> 3 : ");
System.out.print(" Votre choix : ");

choix=clavier.nextInt();

switch(choix) {
case 1:

ajouter();
break;

case 2:

modifier();
break;

case 3:

supprimer();
break;

default:
System.out.print("Fin");

}
}

}
package dao;

import entites.Module;

public class ModuleDAO extends ConnectionBD{


private String table = "Module";

public void ajout(Module er) {


String req = "INSERT INTO " + table + " (code,nomM)
VALUES (" + er.getCode() + ",'" + er.getNomM() + ")";
//System.out.print(req);
this.ouvrir();
this.mAJ(req);

public void supprimer(Integer id) {


String req = "DELETE FROM " + table + " where Code=" +
id;
System.out.print(req);
this.ouvrir();
this.mAJ(req);
}

public void miseAjour(Integer ref, Module et) {

// String req = "UPDATE " + table + " set nomM='" +


et.getNomM()+ "', id=" + et.getCode()+ " where code="
+et.getCode();
String req = "UPDATE " + table + " set nomM='" + et.getNomM()
+"' where code=" + et.getCode();
System.out.print(req);
this.ouvrir();
this.mAJ(req);
}

}
package dao;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class ConnectionBD {

protected static Connection con;

protected Statement st;

protected ResultSet rs;

// ouverure de connexion

public boolean ouvrir() {

boolean flag = false;

try {

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/gcom";

con = DriverManager.getConnection(url, "root", "");

st = con.createStatement();

System.out.println("Connexion ok");

flag = true;

} catch (ClassNotFoundException e) {
System.out.println("Drivers introuvables");

} catch (SQLException e) {

System.out.println("Adresse ou identification incorrecte");

System.out.print(e.getMessage());

} catch (Exception e) {

System.out.print("Erreur de connexion");

System.out.print(e.getMessage());

System.out.println("Connection ok");

return flag;

public void mAJ(String req){

try{

st.executeUpdate(req);

}catch(SQLException e){

System.out.print(e.getMessage());

// afficher liste

protected void afficher(String req){

try{

rs=st.executeQuery(req);
}catch(SQLException e){

System.out.print(e.getMessage());

You might also like