Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- class SQLServerDatabase {
- private String uri;
- private String user;
- private String password;
- private Connection conn;
- public SQLServerDatabase(String uri, String user, String password) {
- this.uri = uri;
- this.user = user;
- this.password = password;
- }
- public boolean connectSQL() {
- String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- // load driver //classpaths... new... add jar
- try{
- Class.forName( driver );
- }
- catch(ClassNotFoundException cnfe){
- return false;
- }
- // connect to the DB
- try{
- this.conn = DriverManager.getConnection( this.uri, this.user, this.password );
- return true;
- }
- catch (SQLException sqle){
- return false;
- }
- }
- public boolean disconnectSQL() {
- // close the DB
- try {
- if (this.conn != null) {
- this.conn.close();
- }
- return true;
- }
- catch(SQLException sqle) {
- return false;
- }
- }
- }
- class Main {
- public static void main(String[] args) {
- //String uri = "jdbc:sqlserver://theodore.ist.rit.edu;databaseName=Jobs";
- String uri = args[0];
- String user = args[1];
- String password = args[2];
- SQLServerDatabase database = new SQLServerDatabase(uri, user, password);
- boolean isConn = database.connectSQL();
- if (isConn) {
- System.out.println("Connected to SQL database");
- }
- else {
- System.out.println("Unable to connect to SQL database");
- }
- boolean isDisconn = database.disconnectSQL();
- if (isDisconn) {
- System.out.println("Disconnected from SQL database");
- }
- else {
- System.out.println("Unable to disconnect from SQL database");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement