Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Wojtek
- */
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author 15
- */
- public class PostgreSQLDatabase {
- public static void connect() {
- System.out.println("-------- PostgreSQL "
- + "JDBC Connection Testing ------------");
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Where is your PostgreSQL JDBC Driver? "
- + "Include in your library path!");
- e.printStackTrace();
- return;
- }
- System.out.println("PostgreSQL JDBC Driver Registered!");
- Connection connection = null;
- try {
- connection = DriverManager.getConnection(
- "jdbc:postgresql://localhost:5432/lokale", "postgres",
- "admin");
- } catch (SQLException e) {
- System.out.println("Connection Failed! Check output console");
- e.printStackTrace();
- return;
- }
- if (connection != null) {
- System.out.println("You made it, take control your database now!");
- } else {
- System.out.println("Failed to make connection!");
- }
- }
- public static void ZapisCaloscBaza()
- {
- try {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO lokal(ID,imie,nazwisko,wspolnota,email,tel1,tel2,ulica,budynek,klatka,mieszkanie,imie2,nazwisko2,email2,pow,sposob,konto,miesieczne,zadluzenie) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)");
- List temp=Global.pierwszy;
- while (temp!=null)
- {
- pstmt.setString(1, temp.lokal.IDLOK);
- pstmt.setString(2, temp.lokal.imie);
- pstmt.setString(3, temp.lokal.nazwisko);
- pstmt.setString(4, temp.lokal.wspolnota);
- pstmt.setString(5, temp.lokal.email);
- pstmt.setString(6, temp.lokal.tel1);
- pstmt.setString(7, temp.lokal.tel2);
- pstmt.setString(8, temp.lokal.ulica);
- pstmt.setInt(9, temp.lokal.budynek);
- pstmt.setString(10, temp.lokal.klatka);
- pstmt.setString(11, temp.lokal.mieszkanie);
- pstmt.setString(12, temp.lokal.imie2);
- pstmt.setString(13, temp.lokal.nazwisko2);
- pstmt.setString(14, temp.lokal.email2);
- pstmt.setDouble(15, temp.lokal.pow);
- pstmt.setInt(16, temp.lokal.sposob);
- pstmt.setString(17, temp.lokal.konto);
- pstmt.setDouble(18, temp.lokal.miesieczne);
- pstmt.setDouble(19, temp.lokal.zadluzenie);
- pstmt.executeUpdate();
- temp=temp.next;
- }
- pstmt.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void Dopisz(Lokal temp)
- {
- try {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO lokal(ID,imie,nazwisko,wspolnota,email,tel1,tel2,ulica,budynek,klatka,mieszkanie,imie2,nazwisko2,email2,pow,sposob,konto,miesieczne,zadluzenie,lastedit) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)");
- pstmt.setString(1, temp.IDLOK);
- pstmt.setString(2, temp.imie);
- pstmt.setString(3, temp.nazwisko);
- pstmt.setString(4, temp.wspolnota);
- pstmt.setString(5, temp.email);
- pstmt.setString(6, temp.tel1);
- pstmt.setString(7, temp.tel2);
- pstmt.setString(8, temp.ulica);
- pstmt.setInt(9, temp.budynek);
- pstmt.setString(10, temp.klatka);
- pstmt.setString(11, temp.mieszkanie);
- pstmt.setString(12, temp.imie2);
- pstmt.setString(13, temp.nazwisko2);
- pstmt.setString(14, temp.email2);
- pstmt.setDouble(15, temp.pow);
- pstmt.setInt(16, temp.sposob);
- pstmt.setString(17, temp.konto);
- pstmt.setDouble(18, temp.miesieczne);
- pstmt.setDouble(19, temp.zadluzenie);
- pstmt.setString(20, Global.PracownikIni);
- pstmt.executeUpdate();
- pstmt.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static int Znajdz(String ids)
- {
- try {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM lokal");
- while(rs.next()) {
- if (rs.getString("id") == null ? ids != null : !rs.getString("id").equals(ids))
- System.out.println(ids+" to nie to -"+rs.getString("id")); else
- {
- System.out.println(ids+" to to -"+rs.getString("id"));
- List temp=new List();
- Lokal temp1=new Lokal();
- temp.lokal=temp1;
- Global.znaleziony=temp;
- Global.znaleziony.lokal.IDLOK=rs.getString("id");
- Global.znaleziony.lokal.imie=rs.getString("imie");
- Global.znaleziony.lokal.nazwisko=rs.getString("nazwisko");
- Global.znaleziony.lokal.wspolnota=rs.getString("wspolnota");
- Global.znaleziony.lokal.email=rs.getString("email");
- Global.znaleziony.lokal.tel1=rs.getString("tel1");
- Global.znaleziony.lokal.tel2=rs.getString("tel2");
- Global.znaleziony.lokal.ulica=rs.getString("ulica");
- Global.znaleziony.lokal.budynek=rs.getInt("budynek");
- Global.znaleziony.lokal.klatka=rs.getString("klatka");
- Global.znaleziony.lokal.mieszkanie=rs.getString("mieszkanie");
- Global.znaleziony.lokal.imie2=rs.getString("imie2");
- Global.znaleziony.lokal.nazwisko2=rs.getString("nazwisko2");
- Global.znaleziony.lokal.email2=rs.getString("email2");
- Global.znaleziony.lokal.pow=rs.getDouble("pow");
- Global.znaleziony.lokal.sposob=rs.getInt("sposob");
- Global.znaleziony.lokal.konto=rs.getString("konto");
- Global.znaleziony.lokal.miesieczne=rs.getDouble("miesieczne");
- Global.znaleziony.lokal.zadluzenie=rs.getDouble("zadluzenie");
- rs.close();
- stmt.close();
- conn.close();
- return 1;
- }
- }
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();}
- return 0;
- }
- public static void DeleteRow(String id)
- {
- try
- {
- Class.forName("org.postgresql.Driver");
- Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- PreparedStatement st = connection.prepareStatement("DELETE FROM lokal WHERE id = ?");
- st.setString(1,id);
- st.executeUpdate();
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- public static void UpdateRow(String id)
- {
- try
- {
- Class.forName("org.postgresql.Driver");
- Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- PreparedStatement st = connection.prepareStatement("UPDATE lokal SET imie = ?, nazwisko =?, wspolnota = ?, email = ?, tel1 = ?, tel2 = ?, ulica = ?, klatka = ?, mieszkanie = ?, imie2 = ?, nazwisko2 = ?, email2= ?, konto= ?, budynek = ?, pow = ?, sposob = ?, miesieczne = ?, zadluzenie = ?, lastedit = ? WHERE id = ?");;
- st.setString(1,Global.znaleziony.lokal.imie);
- st.setString(2,Global.znaleziony.lokal.nazwisko);
- st.setString(3,Global.znaleziony.lokal.wspolnota);
- st.setString(4,Global.znaleziony.lokal.email);
- st.setString(5,Global.znaleziony.lokal.tel1);
- st.setString(6,Global.znaleziony.lokal.tel2);
- st.setString(7,Global.znaleziony.lokal.ulica);
- st.setString(8,Global.znaleziony.lokal.klatka);
- st.setString(9,Global.znaleziony.lokal.mieszkanie);
- st.setString(10,Global.znaleziony.lokal.imie2);
- st.setString(11,Global.znaleziony.lokal.nazwisko2);
- st.setString(12,Global.znaleziony.lokal.email2);
- st.setString(13,Global.znaleziony.lokal.konto);
- st.setInt(14,Global.znaleziony.lokal.budynek);
- st.setDouble(15,Global.znaleziony.lokal.pow);
- st.setInt(16,Global.znaleziony.lokal.sposob);
- st.setDouble(17,Global.znaleziony.lokal.miesieczne);
- st.setDouble(18,Global.znaleziony.lokal.zadluzenie);
- st.setString(19, Global.PracownikIni);
- st.setString(20, id);
- st.executeUpdate();
- connection.close();
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- public static int getSize()
- {
- int i=0;
- try {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM lokal");
- while(rs.next()) {
- i++;
- }
- return i;
- } catch (SQLException e) {
- e.printStackTrace();}
- return i;
- }
- public static List get(int i)
- {
- i++;
- List temp=new List();
- Lokal temp1=new Lokal();
- temp.lokal=temp1;
- try {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = stmt.executeQuery("SELECT * FROM lokal");
- // for (j=1;j<i;j++)
- //{
- rs.absolute(i);
- //}
- temp.lokal.IDLOK=rs.getString("id");
- temp.lokal.imie=rs.getString("imie");
- temp.lokal.nazwisko=rs.getString("nazwisko");
- temp.lokal.wspolnota=rs.getString("wspolnota");
- temp.lokal.email=rs.getString("email");
- temp.lokal.tel1=rs.getString("tel1");
- temp.lokal.tel2=rs.getString("tel2");
- temp.lokal.ulica=rs.getString("ulica");
- temp.lokal.budynek=rs.getInt("budynek");
- temp.lokal.klatka=rs.getString("klatka");
- temp.lokal.mieszkanie=rs.getString("mieszkanie");
- temp.lokal.imie2=rs.getString("imie2");
- temp.lokal.nazwisko2=rs.getString("nazwisko2");
- temp.lokal.email2=rs.getString("email2");
- temp.lokal.pow=rs.getDouble("pow");
- temp.lokal.sposob=rs.getInt("sposob");
- temp.lokal.konto=rs.getString("konto");
- temp.lokal.miesieczne=rs.getDouble("miesieczne");
- temp.lokal.zadluzenie=rs.getDouble("zadluzenie");
- rs.close();
- stmt.close();
- conn.close();
- return temp;
- } catch (SQLException e) {
- e.printStackTrace();}
- return temp;
- }
- public static void pobierzcalosc ()
- {
- try
- {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = stmt.executeQuery("SELECT * FROM lokal");
- List temp=null;
- Lokal temp1=null;
- while (rs.next())
- {
- if (temp==null)
- {
- temp=new List();
- temp1=new Lokal();
- temp.lokal=temp1;
- } else
- {
- temp.next=new List();
- temp.next.lokal=new Lokal();
- temp=temp.next;
- }
- temp.lokal.IDLOK=rs.getString("id");
- temp.lokal.imie=rs.getString("imie");
- temp.lokal.nazwisko=rs.getString("nazwisko");
- temp.lokal.wspolnota=rs.getString("wspolnota");
- temp.lokal.email=rs.getString("email");
- temp.lokal.tel1=rs.getString("tel1");
- temp.lokal.tel2=rs.getString("tel2");
- temp.lokal.ulica=rs.getString("ulica");
- temp.lokal.budynek=rs.getInt("budynek");
- temp.lokal.klatka=rs.getString("klatka");
- temp.lokal.mieszkanie=rs.getString("mieszkanie");
- temp.lokal.imie2=rs.getString("imie2");
- temp.lokal.nazwisko2=rs.getString("nazwisko2");
- temp.lokal.email2=rs.getString("email2");
- temp.lokal.pow=rs.getDouble("pow");
- temp.lokal.sposob=rs.getInt("sposob");
- temp.lokal.konto=rs.getString("konto");
- temp.lokal.miesieczne=rs.getDouble("miesieczne");
- temp.lokal.zadluzenie=rs.getDouble("zadluzenie");
- if (Global.pierwszy==null) Global.pierwszy=temp;
- }
- }
- catch (SQLException e) {
- e.printStackTrace();}
- }
- public static void readStawkiBaza ()
- {
- try
- {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = stmt.executeQuery("SELECT * FROM stawki");
- while (rs.next())
- {
- Global.stawkaconst=rs.getDouble("const");
- Global.stawkam2=rs.getDouble("m2");
- }
- }
- catch (SQLException e) {
- e.printStackTrace();}
- }
- public static void saveStawkiBaza ()
- {
- try {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- PreparedStatement pstmt = conn.prepareStatement("UPDATE stawki SET const = ?, m2= ? WHERE id = 1");
- pstmt.setDouble(1, Global.stawkaconst);
- pstmt.setDouble(2, Global.stawkam2);
- pstmt.executeUpdate();
- pstmt.close();
- conn.close();
- }
- catch (SQLException e) {
- e.printStackTrace();}
- }
- public static int werprac(String id, String nazwisko, String imie)
- {
- //int wystapienie=0;
- try {
- DriverManager.registerDriver(new org.postgresql.Driver());
- Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/lokale", "postgres", "admin");
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM pracownik");
- while(rs.next()) {
- if ((rs.getString("id").equals(id)) && ((!rs.getString("imie").equals(imie)) || (!rs.getString("nazwisko").equals(nazwisko)))) { System.out.println("Już istnieje"); return 1; } else {System.out.println(rs.getString("id")+" to nie to co "+id);}
- }
- return 0;
- } catch (SQLException e) {
- e.printStackTrace();
- return 0;}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement