Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package database;
  6.  
  7. import java.sql.*;
  8.  
  9. /**
  10. *
  11. * @author Cycki
  12. */
  13. public class DataBase {
  14.  
  15. static private Connection conn=null;
  16. static String dbURL=new String("jdbc:postgresql://127.0.0.1:5432/baza");//url z serwerem bd
  17. static Statement sql;
  18.  
  19. public static void main(String[] args) {
  20. try{
  21. //wczytanie sterownika dla bazy danych postgresql
  22. Class d=Class.forName("org.postgresql.Driver");
  23. System.out.println("Wczytano: "+d);
  24.  
  25. //umozliwienie rejestracji
  26. DriverManager.setLogStream(System.err);//sprawia ze wszystkie komunuikaty bedo zapisywane w standardowym steruminiu bledow
  27.  
  28. System.out.println("Nawiazanie polaczenia");
  29. Connection conn=DriverManager.getConnection(dbURL, "postgres", "aaa");
  30.  
  31. //getWarnings - wyswietla dodatkowe inf i ostzezenia z obiektu Connection
  32. SQLWarning warn=conn.getWarnings();
  33. while(warn!=null)
  34. {
  35. System.out.println("Stan SQL: "+ warn.getSQLState());
  36. System.out.println("Komunikat: "+warn.getMessage());
  37. System.out.println("Sprzedawca: "+warn.getErrorCode());
  38. System.out.println("");
  39. warn=warn.getNextWarning();
  40. }
  41.  
  42. sql = conn.createStatement();
  43.  
  44. //Tworzenie tabeli
  45.  
  46. String sqlText = "create table tabela ("
  47. + "IMIE text,"
  48. + "NAZWISKO text,"
  49. + "WIEK integer);";
  50. System.out.println("Executing this command: "+sqlText+"\n");
  51. sql.executeUpdate(sqlText);
  52.  
  53.  
  54. //Dodawanie rekordu
  55. sqlText = "insert into tabela values ('Jan','Kowalski',50)";
  56. System.out.println("Executing this command: "+sqlText+"\n");
  57. sql.executeUpdate(sqlText);
  58.  
  59. sqlText = "insert into tabela values ('Grzegorz','WiÅ?niewski',20)";
  60. System.out.println("Executing this command: "+sqlText+"\n");
  61. sql.executeUpdate(sqlText);
  62.  
  63. sqlText = "insert into tabela values ('Joanna','Nowak',30)";
  64. System.out.println("Executing this command: "+sqlText+"\n");
  65. sql.executeUpdate(sqlText);
  66.  
  67. sqlText = "insert into tabela values ('Krzysztof','Rzeszowski',60)";
  68. System.out.println("Executing this command: "+sqlText+"\n");
  69. sql.executeUpdate(sqlText);
  70.  
  71. sqlText = "insert into tabela values ('Beata','Krakowiak',25)";
  72. System.out.println("Executing this command: "+sqlText+"\n");
  73. sql.executeUpdate(sqlText);
  74.  
  75. // WyÅ?wietlanie tabeli
  76. /* if( sql.execute("select * from tabela") == false )
  77. {
  78. // Get the update count
  79. int num = sql.getUpdateCount() ;
  80.  
  81. System.out.println( num + " rows affected" ) ;
  82. }
  83. else
  84. {
  85. // Get the result set and the metadata
  86. ResultSet rs = sql.getResultSet() ;
  87. ResultSetMetaData md = rs.getMetaData() ;
  88.  
  89. // Loop through the result set
  90. while( rs.next() )
  91. {
  92. for( int i = 1; i <= md.getColumnCount(); i++ )
  93. System.out.print( rs.getString(i) + " " ) ;
  94. System.out.println() ;
  95. }
  96.  
  97. // Close the result set, statement and the connection
  98. rs.close() ;
  99. }*/
  100.  
  101.  
  102. //obsluga polaczenia...
  103. conn.close();
  104. }catch(ClassNotFoundException exc)
  105. {
  106. System.err.println(exc+". Nie mozna pobrac sterownika.");
  107. }
  108. catch(SQLException e)
  109. {
  110. System.out.println("Nie mozna nawiazac poloczenia z BD "+e);
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement