Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=BDProject [sa on db_accessadmin];user=sa;password=sa2012;";
  2.  
  3. Connection con = null;
  4. Statement stmt = null;
  5. ResultSet rs = null;
  6.  
  7. try {
  8. // Establish the connection.
  9. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  10. con = DriverManager.getConnection(connectionUrl);
  11.  
  12. // Create and execute an SQL statement that returns some data.
  13. String SQL = "SELECT TOP 10 * FROM Person.Contact";
  14. stmt = con.createStatement();
  15. rs = stmt.executeQuery(SQL);
  16.  
  17. // Iterate through the data in the result set and display it.
  18. while (rs.next()) {
  19. System.out.println(rs.getString(4) + " " + rs.getString(6));
  20. }
  21. }
  22.  
  23. // Handle any errors that may have occurred.
  24. catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. finally {
  28. if (rs != null) try { rs.close(); } catch(Exception e) {}
  29. if (stmt != null) try { stmt.close(); } catch(Exception e) {}
  30. if (con != null) try { con.close(); } catch(Exception e) {}
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement