Must be done in Java using Netbeans. Here are the necessary files: https://www.m
ID: 3941306 • Letter: M
Question
Must be done in Java using Netbeans. Here are the necessary files: https://www.mediafire.com/?llhfi4wxywh2wnn
Tasks and Rubric
Activity
PostgreSQL database install
Follow the instructions in document PostgreSQL_Install.docx
PostgreSQL database restore
Follow the instructions in document PostgreSQL_Runtime.docx
PostgreSQL database connection to Java
Follow the instructions in document Java_ConnectTo_PostgreSQL.docx
Database project
Create a new Java Application project named Database
allowing Netbeans IDE to create the main class called Database
Database.java
Download properties.xml from assignment folder on Webcourses.
Create an instance of class XmlParser, passing “inputOutput/properties.xml” as an argument to the constructor
Declare a variable of class ConnectionData; instantiate it by setting it equal to the return variable from class XmlParser method getConnectionData()
Create an instance of class PostgreSQLConnect by passing the variable of class ConnectionData as an argument to the constructor
Call method getConnection() using the reference variable created above
PostgreSQLConnect.java
Include member variable Connection connect
Write a constructor to receive a parameter of class ConnectionData
Call static method Class.forName() passing argument getType() method call on the instance of ConnectionData
Method call toString() on class instance ConnectionData
Method call getLogin() on class instance ConnectionData
Method call getPassword() on class instance ConnectionData
catch exception type Exception
output to the user that the database connection was not successful
If the code gets past the try/catch without throwing an exception then the database connection was successful, output to the user that the connection was successful
inputOutput package
Create package inputOutput; copy at the directory structure the properties.xml file downloaded from Webcourses
ConnectionData.java
Create class ConnectionData in package inputOutput; it should do the following:
String type
String url
String ipaddress
String port
String database
String login
String password
Getters/setters for member variables
Method toString() should concatenate member variables url, ipaddress, port, and database so that it is in the format "jdbc:postgresql://localhost:5432/dvdrental"
XmlParser.java
Create class XmlParser in package inputOutput; it should do the following:
ConnectionData connectionData
Document document
Getter for member variable connectionData
Defined one parameter of type String storing the name of the XML file to parse
Call method parseXmlFile() passing the parameter as an argument
return type void
Define one parameter of type String storing the name of the XML file to parse
Create an instance of class DocumentBuilderFactory
Create an instance of class DocumentBuilder
Instantiate member variable document to the parse method of class DocumentBuilder passing argument ClassLoader.getSystemResourceAsStream() passing the parameter for method parseXmlFile as an argument
Create an instance of class NodeList, instantiate it with method getDocumentElement().getChildNodes() from member variable document
Loop through the length of the NodeList instance
i.Create an instance of class Node, instantiate it to method item() of instance NodeList
ii.Get each element from XML element <driver>
iii.Instantiate member variable connectionData setting each of its member variables relative to the elements in the properties.xml file
properties.xml
Put file in the inputOutput package
Database application
Test Case 1
Test Case 1 passes
Test Case 2
Test Case 2 passes
Source compiles with no errors
Source runs with no errors
Source includes comments
Total
Perform the following test cases
Test Cases
Action
Expected outcome
Test Case 1
Parse XML file
File was successfully parsed
Test Case 2
Connect to database dvdrental
Database connection was successful using properties.xml file data
Activity
PostgreSQL database install
Follow the instructions in document PostgreSQL_Install.docx
PostgreSQL database restore
Follow the instructions in document PostgreSQL_Runtime.docx
PostgreSQL database connection to Java
Follow the instructions in document Java_ConnectTo_PostgreSQL.docx
Database project
Create a new Java Application project named Database
allowing Netbeans IDE to create the main class called Database
Database.java
Download properties.xml from assignment folder on Webcourses.
Create an instance of class XmlParser, passing “inputOutput/properties.xml” as an argument to the constructor
Declare a variable of class ConnectionData; instantiate it by setting it equal to the return variable from class XmlParser method getConnectionData()
Create an instance of class PostgreSQLConnect by passing the variable of class ConnectionData as an argument to the constructor
Call method getConnection() using the reference variable created above
PostgreSQLConnect.java
Include member variable Connection connect
Write a constructor to receive a parameter of class ConnectionData
Inside a try blockCall static method Class.forName() passing argument getType() method call on the instance of ConnectionData
Set variable connect equal to static method call DriverManager.getConnection() passing the following argumentsMethod call toString() on class instance ConnectionData
Method call getLogin() on class instance ConnectionData
Method call getPassword() on class instance ConnectionData
Inside catch blockcatch exception type Exception
output to the user that the database connection was not successful
If the code gets past the try/catch without throwing an exception then the database connection was successful, output to the user that the connection was successful
inputOutput package
Create package inputOutput; copy at the directory structure the properties.xml file downloaded from Webcourses
ConnectionData.java
Create class ConnectionData in package inputOutput; it should do the following:
Member variableString type
String url
String ipaddress
String port
String database
String login
String password
Getters/setters for member variables
Method toString() should concatenate member variables url, ipaddress, port, and database so that it is in the format "jdbc:postgresql://localhost:5432/dvdrental"
XmlParser.java
Create class XmlParser in package inputOutput; it should do the following:
Member variableConnectionData connectionData
Document document
Getter for member variable connectionData
Constructor should do the following:Defined one parameter of type String storing the name of the XML file to parse
Call method parseXmlFile() passing the parameter as an argument
Method parseXmlFile() should do the following:return type void
Define one parameter of type String storing the name of the XML file to parse
Create an instance of class DocumentBuilderFactory
Create an instance of class DocumentBuilder
Instantiate member variable document to the parse method of class DocumentBuilder passing argument ClassLoader.getSystemResourceAsStream() passing the parameter for method parseXmlFile as an argument
Create an instance of class NodeList, instantiate it with method getDocumentElement().getChildNodes() from member variable document
Loop through the length of the NodeList instance
i.Create an instance of class Node, instantiate it to method item() of instance NodeList
ii.Get each element from XML element <driver>
iii.Instantiate member variable connectionData setting each of its member variables relative to the elements in the properties.xml file
properties.xml
Put file in the inputOutput package
Database application
Test Case 1
Test Case 1 passes
Test Case 2
Test Case 2 passes
Source compiles with no errors
Source runs with no errors
Source includes comments
Total
Explanation / Answer
Database.java
import inputOutput.ConnectionData;
import inputOutput.XmlParser;
public class Database {
public static void main(String[] args) {
XmlParser parser = new XmlParser("src/inputOutput/properties.xml");
parser.parseXml();
ConnectionData connectionData = parser.getConnectionData();
PostgreSQLConnect postgreSQLConnect = new PostgreSQLConnect(connectionData);
postgreSQLConnect.getConnection();
}
}
ConnectionData.java
package inputOutput;
public class ConnectionData {
String type;
String url;
String ipaddress;
String port;
String database;
String login;
String password;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getIpaddress() {
return ipaddress;
}
public void setIpaddress(String ipaddress) {
this.ipaddress = ipaddress;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return url+"://"+ipaddress+":"+port+"/"+database;
}
}
XmlParser.java
package inputOutput;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlParser {
ConnectionData connectionData;
String fileName;
public XmlParser(String fileName) {
super();
this.fileName = fileName;
}
public ConnectionData getConnectionData() {
return connectionData;
}
public void parseXml(){
try {
File inputFile = new File(fileName);//"src/inputOutput/properties.xml");
connectionData = new ConnectionData();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nListroot = doc.getElementsByTagName("properties");
for(int temp1 = 0; temp1 < nListroot.getLength(); temp1++){
Node nNode1 = nListroot.item(temp1);
if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement1 = (Element) nNode1;
}
}
NodeList nList = doc.getElementsByTagName("driver");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String type = eElement.getAttribute("type");
connectionData.setType(type);
connectionData.setUrl(eElement.getElementsByTagName("url").item(0).getTextContent());
connectionData.setIpaddress(eElement.getElementsByTagName("ipaddress").item(0).getTextContent());
connectionData.setPort(eElement.getElementsByTagName("port").item(0).getTextContent());
connectionData.setDatabase(eElement.getElementsByTagName("database").item(0).getTextContent());
connectionData.setLogin(eElement.getElementsByTagName("login").item(0).getTextContent());
connectionData.setPassword(eElement.getElementsByTagName("password").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
PostgreSQLConnect.java
import java.sql.Connection;
import java.sql.DriverManager;
import inputOutput.ConnectionData;
public class PostgreSQLConnect {
ConnectionData connectionData;
public PostgreSQLConnect(ConnectionData connectionData) {
this.connectionData = connectionData;
}
public Connection getConnection(){
Connection connection= null;
try{
Class.forName(connectionData.getType());
connection = DriverManager.getConnection(connectionData.toString(),connectionData.getLogin(),connectionData.getPassword());
System.out.println("Connection was successfull");
}catch(Exception e){
e.printStackTrace();
System.out.println("Connection was not successfull");
}
return connection;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.