Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package SqlConnection;
  2.  
  3. /**
  4.  * Created by Sergio on 6/8/2016.
  5.  */
  6.  
  7. import java.sql.*;
  8. import java.sql.ResultSet;
  9.  
  10.  
  11. public class SqlConnection {
  12.  
  13.     //Ponemos las variables globales dentro de la clase para poder accesarlas
  14.  
  15.     String strConnectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=Arkus_Test;user=sa;password=7g8hc9trn";
  16.     String strQuery = "SELECT * FROM Mails";
  17.     String strQueryResult = null;
  18.  
  19.     Connection obj_Conection;
  20.     ResultSet Obj_Rs;
  21.  
  22.     public SqlConnection(){
  23.         try{
  24.             connectAndMakeQuery();
  25.         }catch(Exception e){}
  26.     }
  27.  
  28.     public void connectAndMakeQuery() throws SQLException, ClassNotFoundException {
  29.         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  30.  
  31.         obj_Conection = DriverManager.getConnection(strConnectionUrl);
  32.         System.out.println("DB Connected");
  33.         Statement Obj_Sta = obj_Conection.createStatement();
  34.         Obj_Rs = Obj_Sta.executeQuery(strQuery);
  35.     }
  36.  
  37.     public Entry<String,String> getNextData() {
  38.         if(Obj_Rs.next()){
  39.             return new Abstract.SimpleEntry<String,String>(Obj_Rs.getString("Mail"), Obj_Rs.getString("Password"));
  40.         }
  41.         return null;
  42.     }
  43.  
  44.     public void close(){
  45.         obj_Conection.close();
  46.     }
  47.  
  48. //    public String GetValueFromDB() throws SQLException, ClassNotFoundException {
  49. //
  50. //        String strConnectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=Arkus_Test;user=sa;password=7g8hc9trn";
  51. ////        String strConnectionUrl = "jdbc:sqlserver://172.20.10.68:1433;databaseName=Arkus_Test;user=ArkusTest;password=Arkus123";
  52. //        String strQuery = "SELECT * FROM Mails";
  53. //        String strQueryResult = null;
  54. //
  55. //        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  56. //
  57. //        Connection obj_Conection = DriverManager.getConnection(strConnectionUrl);
  58. //        System.out.println("DB Connected");
  59. //        Statement Obj_Sta = obj_Conection.createStatement();
  60. //        ResultSet Obj_Rs = Obj_Sta.executeQuery(strQuery);
  61. //
  62. //        if (Obj_Rs.next()) {
  63. //            while (Obj_Rs.next()) {
  64. //                System.out.println(Obj_Rs.getString("Mail"));
  65. //                strQueryResult = Obj_Rs.getString("Mail");
  66. //            }
  67. //
  68. //            obj_Conection.close();
  69. //
  70. //        }
  71. //
  72. //
  73. //        return strQueryResult;
  74. //    }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement