-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetConnection.java
More file actions
97 lines (85 loc) · 3.46 KB
/
getConnection.java
File metadata and controls
97 lines (85 loc) · 3.46 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kitordersystem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
/**
* This class gets all the connection properties by opening an XML file (set
* as kitorder.xml) and then use the nodes in that to connect to the
* database itself
*/
public class getConnection {
public String dbms;
public String dbName;
public String userName;
public String password;
private String serverName;
private int portNumber;
/**
* @return
* @throws SQLException
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public Connection getConnection() throws SQLException, IOException,
SAXException, ParserConfigurationException {
Connection conn = null;
this.setProperties("/Users/tsmoffat/kitordersystem/kitordersystem/" +
"kitordersystem/kitorder.xml");
String JDBC_URL = "jdbc:" + dbms + "://" + serverName + ":" + "3306"
+ "/";
System.out.println("Connecting to: " + dbms + "://" + serverName +
":3306");
conn = DriverManager.getConnection(JDBC_URL, userName, password);
return conn;
}
/**
* @param fileName - The file where the connection details can be found,
* it's easier to pass it through from elsewhere
* @throws FileNotFoundException
* @throws IOException
* @throws InvalidPropertiesFormatException
* @throws SAXException
* @throws ParserConfigurationException
*/
public void setProperties(String fileName) throws FileNotFoundException,
IOException, InvalidPropertiesFormatException, SAXException,
ParserConfigurationException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fileName);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("database");
Element database = (Element) nList.item(0);
Element connection = (Element) database.getElementsByTagName
("connection").item(0);
this.dbms = connection.getElementsByTagName("dbms").item(0).
getTextContent();
this.dbName = connection.getElementsByTagName("database_name").item
(0).getTextContent();
this.userName = connection.getElementsByTagName("user_name").item(0).
getTextContent();
this.password = connection.getElementsByTagName("password").item(0).
getTextContent();
this.serverName = connection.getElementsByTagName("server_name").
item(0).getTextContent();
this.portNumber = Integer.parseInt(connection.getElementsByTagName
("port_number").item(0).getTextContent());
}
}