-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.java
More file actions
64 lines (56 loc) · 1.5 KB
/
Singleton.java
File metadata and controls
64 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package DataAccess;
import Exceptions.DataException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Singleton
{
private static Connection connexionUnique;
public static Connection getInstance() throws DataException
{
if (connexionUnique == null)
{
try
{
Context ctx = new InitialContext();
DataSource source = (DataSource)ctx.lookup("jdbc/BrassLagneaux");
connexionUnique = source.getConnection();
}
catch(SQLException ex)
{
throw new DataException();
}
catch(NamingException ex)
{
throw new DataException();
}
}
return connexionUnique;
}
public static void fermerConnexion() throws DataException
{
if(connexionUnique != null)
{
try
{
connexionUnique.close();
}
catch(SQLException e)
{
throw new DataException();
}
}
}
}
---------------------------------------------------------------------
package Exceptions;
public class DataException extends Exception
{
public String getMessage ()
{
return "Une erreur est survenue lors de la connexion";
}
}