-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConnectionFactory.java
More file actions
78 lines (66 loc) · 2.51 KB
/
ConnectionFactory.java
File metadata and controls
78 lines (66 loc) · 2.51 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package exemplo05mysql.db;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* <p>
* O arquivo resources/lab01-mysql-dml-ddl.sql contém as instruções DML e DDL
* para criação
* do banco de dados necessário para esse exemplo. Crie um esquema em uma
* instalação
* MySQL / MariaDB e importe o conteúdo desse arquivo.
* <p>
* Faça os ajustes de configuração de conexão nas variáveis dentro do Construtor
* dessa classe.
* <p>
* Verifique se sua instalação MySQL permite receber conexões por meio de
* sockets TCP
*/
public abstract class ConnectionFactory {
private static final String DB_PROPERTIES_FILE = "database.properties";
private static Connection cnx;
/**
* Para carregar o arquivo properties que está no diretório resources,
* funciona dentro da IDEA, nos teste de unidade e dentro de arquivo JAR
*
* @return
* @throws IOException
*/
private static InputStream getInputStream() throws IOException {
InputStream is = ConnectionFactory.class.getClassLoader().getResourceAsStream(DB_PROPERTIES_FILE);
if (is == null) {
throw new IOException("arquivo não encontrado " + DB_PROPERTIES_FILE);
} else {
return is;
}
}
/**
* Faz a conexão em um banco de dados MySQL e retorna o objeto Connection
*
* @return conexão com um banco MySQL
* @throws IOException
* @throws SQLException
*/
public static synchronized Connection getDBConnection() throws IOException, SQLException {
Properties properties = new Properties();
try {
// carregando as propriedades user, password e useSSL do arquivo
// database.properties
properties.load(getInputStream());
// obtendo valores para host, port e dbname do arquivo properties
String host = properties.getProperty("host");
String port = properties.getProperty("port");
String dbname = properties.getProperty("database");
String url = "jdbc:mysql://" + host + ":" + port + "/" + dbname;
cnx = DriverManager.getConnection(url, properties);
} catch (SQLException ex) {
throw new SQLException("erro com instrução SQL", ex);
} catch (IOException ex) {
throw new IOException("arquivo properties não encontrado", ex);
}
return cnx;
}
}