algui91

SQLServer-Java

Apr 27th, 2011
11,268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.sql.Statement;
  2.  
  3. public class Test {
  4.     private java.sql.Connection connection = null;
  5.     private final String url = "jdbc:microsoft:sqlserver://";
  6.     private final String serverName = "192.168.1.38";
  7.     private final String portNumber = "1433";
  8.     private final String databaseName = "db_WifiBar";
  9.     private final String userName = "algui91";
  10.     private final String password = "1234";
  11.     private final String statement = "select * from prueba;";
  12.     // Informs the driver to use server a side-cursor,
  13.     // which permits more than one active statement
  14.     // on a connection.
  15.     private final String selectMethod = "Direct";
  16.  
  17.     // Constructor
  18.     public Test() {
  19.     }
  20.  
  21.     private String getConnectionUrl() {
  22.         return url + serverName + ":" + portNumber + ";databaseName="
  23.                 + databaseName + ";selectMethod=" + selectMethod + ";";
  24.     }
  25.  
  26.     private java.sql.Connection getConnection() {
  27.         try {
  28.             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  29.             connection = java.sql.DriverManager.getConnection(getConnectionUrl(),
  30.                     userName, password);
  31.             if (connection != null)
  32.                 System.out.println("Connection Successful!");
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.             System.out.println("Error Trace in getConnection() : "
  36.                     + e.getMessage());
  37.         }
  38.         return connection;
  39.     }
  40.  
  41.     /*
  42.      * Display the driver properties, database details
  43.      */
  44.  
  45.     public void displayDbProperties() {
  46.         java.sql.DatabaseMetaData dm = null;
  47.         java.sql.ResultSet result = null;
  48.         try {
  49.             connection = this.getConnection();
  50.             if (connection != null) {
  51.                 dm = connection.getMetaData();
  52.                 System.out.println("Driver Information");
  53.                 System.out.println("\tDriver Name: " + dm.getDriverName());
  54.                 System.out
  55.                         .println("\tDriver Version: " + dm.getDriverVersion());
  56.                 System.out.println("\nDatabase Information ");
  57.                 System.out.println("\tDatabase Name: "
  58.                         + dm.getDatabaseProductName());
  59.                 System.out.println("\tDatabase Version: "
  60.                         + dm.getDatabaseProductVersion());
  61.                
  62.                 Statement select = connection.createStatement();
  63.                 result = select.executeQuery(statement);
  64.                
  65.                 while (result.next()) {
  66.                     System.out.println("Nombre: " + result.getString(1) + "\n");
  67.                     System.out.println("Apellido: " + result.getString(2) + "\n");
  68.                     System.out.println("Dni: " + result.getString(3) + "\n");
  69.                 }
  70.                 result.close();
  71.                 result = null;
  72.                 closeConnection();
  73.             } else
  74.                 System.out.println("Error: No active Connection");
  75.         } catch (Exception e) {
  76.             e.printStackTrace();
  77.         }
  78.         dm = null;
  79.     }
  80.  
  81.     private void closeConnection() {
  82.         try {
  83.             if (connection != null)
  84.                 connection.close();
  85.             connection = null;
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.         }
  89.     }
  90.  
  91.     public static void main(String[] args) throws Exception {
  92.         Test myDbTest = new Test();
  93.         myDbTest.displayDbProperties();
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment