Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. class SQLServerDatabase {
  4. private String uri;
  5. private String user;
  6. private String password;
  7. private Connection conn;
  8.  
  9. public SQLServerDatabase(String uri, String user, String password) {
  10. this.uri = uri;
  11. this.user = user;
  12. this.password = password;
  13. }
  14.  
  15. public static void main(String[] args) {
  16. System.out.println("This is a main, hallo");
  17. }
  18.  
  19. public boolean connectSQL() {
  20. String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  21.  
  22. // load driver //classpaths... new... add jar
  23. try{
  24. Class.forName( driver );
  25. }
  26. catch(ClassNotFoundException cnfe){
  27. return false;
  28. }
  29.  
  30. // connect to the DB
  31. try{
  32. this.conn = DriverManager.getConnection( this.uri, this.user, this.password );
  33. return true;
  34. }
  35. catch (SQLException sqle){
  36. return false;
  37. }
  38. }
  39.  
  40. public boolean disconnectSQL() {
  41. // close the DB
  42. try {
  43. if (this.conn != null) {
  44. this.conn.close();
  45. }
  46. return true;
  47. }
  48. catch(SQLException sqle) {
  49. return false;
  50. }
  51. }
  52. }
  53.  
  54. class Main {
  55. public static void main(String[] args) {
  56. //String uri = "jdbc:sqlserver://theodore.ist.rit.edu;databaseName=Jobs";
  57.  
  58. String uri = args[0];
  59. String user = args[1];
  60. String password = args[2];
  61.  
  62. SQLServerDatabase database = new SQLServerDatabase(uri, user, password);
  63. boolean isConn = database.connectSQL();
  64.  
  65. if (isConn) {
  66. System.out.println("Connected to SQL database");
  67. }
  68. else {
  69. System.out.println("Unable to connect to SQL database");
  70. }
  71.  
  72. boolean isDisconn = database.disconnectSQL();
  73.  
  74. if (isDisconn) {
  75. System.out.println("Disconnected from SQL database");
  76. }
  77. else {
  78. System.out.println("Unable to disconnect from SQL database");
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement