Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. package pckg1;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DB{
  6. private Connection conn = null;
  7. private Statement stmt = null;
  8. private ResultSet rs = null;
  9.  
  10. public void connect(){
  11. try {
  12. Class.forName("com.mysql.jdbc.Driver").newInstance();
  13. conn =
  14. DriverManager.getConnection("jdbc:mysql://mysql.agh.edu.pl/tomaada","tomaada","R2jqv8np");
  15. } catch (SQLException ex) {
  16. // handle any errors
  17. System.out.println("SQLException: " + ex.getMessage());
  18. System.out.println("SQLState: " + ex.getSQLState());
  19. System.out.println("VendorError: " + ex.getErrorCode());
  20. }catch(Exception e){e.printStackTrace();}
  21. }
  22. public void listNames(){
  23. try {
  24. connect();
  25. stmt = conn.createStatement();
  26.  
  27. // Wyciagamy wszystkie pola z kolumny name
  28. // znajdujące się w tabeli users
  29. rs = stmt.executeQuery("SELECT authors FROM books");
  30.  
  31. while(rs.next()){
  32. String author = rs.getString(1);
  33. System.out.println("Uzytkownik: "+author);
  34. }
  35. }catch (SQLException ex){
  36. // handle any errors
  37.  
  38. }finally {
  39. // zwalniamy zasoby, które nie będą potrzebne
  40. if (rs != null) {
  41. try {
  42. rs.close();
  43. } catch (SQLException sqlEx) { } // ignore
  44. rs = null;
  45. }
  46.  
  47. if (stmt != null) {
  48. try {
  49. stmt.close();
  50. } catch (SQLException sqlEx) { } // ignore
  51.  
  52. stmt = null;
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement