Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.sql.*;
  7.  
  8.  
  9. public class Baza_danych
  10. {
  11.  
  12.  
  13. static String daneZBazy;
  14.  
  15.  
  16. public static void laczenie(int polaczenie, int tablica)
  17. {
  18.  
  19. //String polaczenieURL = "jdbc:mysql://157.158.81.79/test?user=psmorczewski&password=3uSMk2Yu2v4pbgwL";
  20. //Tworzymy proste zapytanie do bazy danych
  21. //final String query = "Select * FROM pracownicy";
  22. final String port="33306";
  23. //user final String nazwa_uzytkownika="psmorczewski";
  24. //base name final String nazwa_bazy="psmorczewski";
  25. //password final String haslo="3uSMk2Yu2v4pbgwL";
  26. final String ip="157.158.81.79";
  27.  
  28.  
  29. try // Polaczenie do bazy danych ustawienie sterownika jdbc oraz
  30. {
  31. Class.forName("com.mysql.jdbc.Driver");
  32. Connection laczenie=DriverManager.getConnection("jdbc:mysql://157.158.81.79" + port + "/" + nazwa_bazy, nazwa_uzytkownika, haslo);
  33. System.out.println("Prawidlowo polaczylem sie za baza danych");
  34. laczenie.close();
  35.  
  36. }
  37.  
  38. catch(ClassNotFoundException wyjatek)
  39. {
  40. System.out.println("Problem ze sterownikiem");
  41.  
  42. }
  43.  
  44. catch(SQLException wyjatek)
  45. {
  46. //e.printStackTrace();
  47.  
  48. System.out.println("Problem z logowaniem\nProsze sprawdzic:\n nazwę użytkownika, hasło, nazwę bazy danych lub adres IP serwera");
  49. System.out.println("SQLException: " + wyjatek.getMessage());
  50. System.out.println("SQLState: " + wyjatek.getSQLState());
  51. System.out.println("VendorError: " + wyjatek.getErrorCode());
  52. }
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. static void wyswietlDaneZBazy(ResultSet rs)
  62. {
  63. try
  64. {
  65. daneZBazy = rs.getString(1);
  66. System.out.println("\n" + daneZBazy + " ");
  67. daneZBazy = rs.getString(2);
  68. System.out.println(daneZBazy + " ");
  69. daneZBazy = rs.getString(3);
  70. System.out.println(daneZBazy);
  71. }
  72. catch(SQLException e)
  73. {
  74. e.printStackTrace();
  75. }
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement