import java.sql.*; public class TestConnect { public static void main(String []
ID: 3747553 • Letter: I
Question
import java.sql.*;
public class TestConnect {
public static void main(String [] args){
// Definitions for Microsoft (MS) SQL Server connection
String uri = "jdbc:sqlserver://theodore.ist.rit.edu;databaseName=Jobs";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String user = "330User";
String password= "330Password";
// Connection object to database
Connection conn = null;
// Load the database driver
try {
Class.forName( driver );
System.out.println("Driver loaded");
}
catch( ClassNotFoundException cnfe ){
System.out.println("Cannot find or load driver "+driver);
cnfe.printStackTrace();
System.exit(1);
}
// Open the database
try{
conn = DriverManager.getConnection( uri, user, password );
System.out.println("Database open");
}
catch(SQLException sqle){
System.out.println("Could not connect to DB: "+uri);
sqle.printStackTrace();
System.exit(2);
}
// Close the database
try{
conn.close();
System.out.println("Database closed");
}
catch(SQLException sqle){
System.out.println("Could not close database");
sqle.printStackTrace();
System.exit(3);
}
} // end main
} // end TestConnect
How can I use this code to change to the question request.
1. Create a data layer class file named "SQLServerDatabase". The SQLServerDatabase class file should have attribut2es for each of the connection parameters. These parameters can be set in the constructor or when they are declared a. b. There should also be a "connection" attribute to hold the DB connection object. There should be a public "connect" method. This method will attempt to connect to the database. If successful, it should set the connection attribute and return true. If unsuccessful, it should return false. Finally, there should be a "close" method that closes the connection and returns true or false depending on the success of the close. c. d.Explanation / Answer
Please find the modified code below.
CODE
====================
import java.sql.*;
public class SQLServerDatabase {
String uri, driver, user, password;
Connection connection;
public SQLServerDatabase(String uri, String driver, String user, String password) {
super();
this.uri = uri;
this.driver = driver;
this.user = user;
this.password = password;
this.connection = null;
}
public boolean connect() {
// Load the database driver
try {
Class.forName( driver );
System.out.println("Driver loaded");
}
catch( ClassNotFoundException cnfe ){
System.out.println("Cannot find or load driver "+driver);
return false;
}
// Open the database
try{
connection = DriverManager.getConnection(this.uri, this.user, this.password );
return true;
}
catch(SQLException sqle){
System.out.println("Could not connect to DB: " + uri);
return false;
}
}
public boolean close() {
try{
connection.close();
return true;
}
catch(SQLException sqle){
System.out.println("Could not close database!!");
return false;
}
}
public static void main(String [] args){
// Definitions for Microsoft (MS) SQL Server connection
String uri = "jdbc:sqlserver://theodore.ist.rit.edu;databaseName=Jobs";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String user = "330User";
String password= "330Password";
SQLServerDatabase db = new SQLServerDatabase(uri, driver, user, password);
// Connection object to database
db.connect();
// Close the database
db.close();
} // end main
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.