Advertisement
Guest User

Untitled

a guest
Jan 14th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package teatrconnect;
  2.  
  3. import java.sql.*;  
  4.  
  5.         public class TeatrConnect {  
  6.             public static void main(String[] args) {  
  7.                 System.out.println("jdbc:sqlserver://KOMPUTER\\SQLEXPRESS:1433");
  8.                 String connectionString =  
  9.                     "jdbc:sqlserver://KOMPUTER\\SQLEXPRESS:1433;"  
  10.                     + "database=Teatr;"  
  11.                      + "user=sa;"  
  12.                     + "password=Czekolaada1;" ;
  13.                 // Zadeklarowanie obiektów JDBC  
  14.                 Connection connection = null;  
  15.                 Statement statement = null;  
  16.                 ResultSet resultSet = null;  
  17.  
  18.                 try {  
  19.                     connection = DriverManager.getConnection(connectionString);  
  20.  
  21.                     //polecenia SQL
  22.                     String selectSql = "SELECT * from Sala";  
  23.                     statement = connection.createStatement();  
  24.                     resultSet = statement.executeQuery(selectSql);  
  25.  
  26.                     // Wyświetlenie wyników
  27.                     while (resultSet.next())  
  28.                     {  
  29.                         System.out.println(resultSet.getString(2));
  30.                            
  31.                     }  
  32.                 }  
  33.                 catch (Exception e) {  
  34.                     e.printStackTrace();  
  35.                 }  
  36.                 finally {  
  37.                      
  38.                     if (resultSet != null) try { resultSet.close(); } catch(Exception e) {}  
  39.                     if (statement != null) try { statement.close(); } catch(Exception e) {}  
  40.                     if (connection != null) try { connection.close(); } catch(Exception e) {}  
  41.                 }  
  42.             }  
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement