Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package database;
- import java.sql.*;
- /**
- *
- * @author Cycki
- */
- public class DataBase {
- static private Connection conn=null;
- static String dbURL=new String("jdbc:postgresql://127.0.0.1:5432/baza");//url z serwerem bd
- static Statement sql;
- public static void main(String[] args) {
- try{
- //wczytanie sterownika dla bazy danych postgresql
- Class d=Class.forName("org.postgresql.Driver");
- System.out.println("Wczytano: "+d);
- //umozliwienie rejestracji
- DriverManager.setLogStream(System.err);//sprawia ze wszystkie komunuikaty bedo zapisywane w standardowym steruminiu bledow
- System.out.println("Nawiazanie polaczenia");
- Connection conn=DriverManager.getConnection(dbURL, "postgres", "aaa");
- //getWarnings - wyswietla dodatkowe inf i ostzezenia z obiektu Connection
- SQLWarning warn=conn.getWarnings();
- while(warn!=null)
- {
- System.out.println("Stan SQL: "+ warn.getSQLState());
- System.out.println("Komunikat: "+warn.getMessage());
- System.out.println("Sprzedawca: "+warn.getErrorCode());
- System.out.println("");
- warn=warn.getNextWarning();
- }
- sql = conn.createStatement();
- //Tworzenie tabeli
- String sqlText = "create table tabela ("
- + "IMIE text,"
- + "NAZWISKO text,"
- + "WIEK integer);";
- System.out.println("Executing this command: "+sqlText+"\n");
- sql.executeUpdate(sqlText);
- //Dodawanie rekordu
- sqlText = "insert into tabela values ('Jan','Kowalski',50)";
- System.out.println("Executing this command: "+sqlText+"\n");
- sql.executeUpdate(sqlText);
- sqlText = "insert into tabela values ('Grzegorz','WiÅ?niewski',20)";
- System.out.println("Executing this command: "+sqlText+"\n");
- sql.executeUpdate(sqlText);
- sqlText = "insert into tabela values ('Joanna','Nowak',30)";
- System.out.println("Executing this command: "+sqlText+"\n");
- sql.executeUpdate(sqlText);
- sqlText = "insert into tabela values ('Krzysztof','Rzeszowski',60)";
- System.out.println("Executing this command: "+sqlText+"\n");
- sql.executeUpdate(sqlText);
- sqlText = "insert into tabela values ('Beata','Krakowiak',25)";
- System.out.println("Executing this command: "+sqlText+"\n");
- sql.executeUpdate(sqlText);
- // WyÅ?wietlanie tabeli
- /* if( sql.execute("select * from tabela") == false )
- {
- // Get the update count
- int num = sql.getUpdateCount() ;
- System.out.println( num + " rows affected" ) ;
- }
- else
- {
- // Get the result set and the metadata
- ResultSet rs = sql.getResultSet() ;
- ResultSetMetaData md = rs.getMetaData() ;
- // Loop through the result set
- while( rs.next() )
- {
- for( int i = 1; i <= md.getColumnCount(); i++ )
- System.out.print( rs.getString(i) + " " ) ;
- System.out.println() ;
- }
- // Close the result set, statement and the connection
- rs.close() ;
- }*/
- //obsluga polaczenia...
- conn.close();
- }catch(ClassNotFoundException exc)
- {
- System.err.println(exc+". Nie mozna pobrac sterownika.");
- }
- catch(SQLException e)
- {
- System.out.println("Nie mozna nawiazac poloczenia z BD "+e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement