Witam,
udało mi się stworzyć dwa osobno działające programy, lecz teraz potrzebuję je razem złączyć lecz niestety nie wiem jak.., mógłby ktoś mi pomóc z tym?
Chodzi o to by w gui można dodawać rekordy do bazy danych oraz wyświetlać je, w w klasie "ZakladPogrzebowy" udało mi się to stworzyć, oraz w gui udało mi się stworzyć dodawanie rekordów do samej tabeli (lecz bez powiązania z bazą danych).
Przydałoby się by łączenie z bazą danych i główne operacje były zachowane w klasie "ZakladPogrzebowy" a w gui tylko pobieranie wartości do niej.
Jeszcze jedno pytanie, czy jest możliwość by dodając do bazy wartości w polskimi znakami były takie dodawane? bo obecnie wyświetlają się kwadraty w tych miejscach.
Kod ZakladPogrzebowy:
/*
* 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.
*/
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
import zakladpogrzebowy.GUI;
/**
*
* @author Rafał
*/
public class ZakladPogrzebowy {
static String daneZBazy;
/**
* @param args
*/
// public static void main(String[] args) {
//
//
// dodajDaneDoBazy();
// // wyswietlDaneZBazy();
//
//
// }
static void dodajDaneDoBazy(){
Scanner odczyt = new Scanner(System.in);
System.out.println("\n Podaj id klienta: ");
int id_klienta =odczyt.nextInt();
Scanner odczyt1 = new Scanner(System.in);
System.out.println("Podaj imię klienta: ");
String imie =odczyt.next();
Scanner odczyt2 = new Scanner(System.in);
System.out.println("Podaj nazwisko klienta: ");
String nazwisko =odczyt.next();
Scanner odczyt3 = new Scanner(System.in);
System.out.println("Podaj adres klienta: ");
String adres =odczyt.next();
Scanner odczyt4 = new Scanner(System.in);
System.out.println("Podaj miasto klienta: ");
String miasto =odczyt.next();
Scanner odczyt5 = new Scanner(System.in);
System.out.println("Podaj numer telefonu klienta: ");
String telefon =odczyt.next();
String dodaj = "INSERT INTO public.\"Klient\" "+ "VALUES ("+ id_klienta + ",'"+imie+"','"+nazwisko+"','"+adres+"','"+miasto+"',"+telefon+")";
String polaczenieURL = "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=123456";
//Tworzymy proste zapytanie do bazy danych
Connection conn = null;
try {
//Ustawiamy dane dotyczące podłączenia
conn = DriverManager.getConnection(polaczenieURL);
//Ustawiamy sterownik MySQL
Class.forName("com.mysql.jdbc.Driver");
//Uruchamiamy zapytanie do bazy danych
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(dodaj);
// stmt.executeUpdate(dodaj);
conn.close();
}
//Wyrzuć wyjątki jężeli nastąpią błędy z podłączeniem do bazy danych lub blędy zapytania o dane
catch(ClassNotFoundException wyjatek) {
System.out.println("Problem ze sterownikiem");
}
catch(SQLException wyjatek) {
// e.printStackTrace();
// System.out.println("Problem z logowaniem\nProsze sprawdzic:\n nazwę użytkownika, hasło, nazwę bazy danych lub adres IP serwera");
System.out.println("SQLException: " + wyjatek.getMessage());
System.out.println("SQLState: " + wyjatek.getSQLState());
System.out.println("VendorError: " + wyjatek.getErrorCode());
}
// String dodaj2 = "\"INSERT INTO public.\"Klient\"\"+\"VALUES (\"+ dodajDaneDoBazy(id_klienta)+\"'Adam','Kowalski','Lwoska','Kraków',888999666)\"";
// stmt.executeUpdate("INSERT INTO public.\"Klient\""+"VALUES ("+ id_klienta+",'Adam','Kowalski','Lwoska','Kraków',888999666)");
}
static void wyswietlDaneZBazy(){
String polaczenieURL = "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=123456";
//Tworzymy proste zapytanie do bazy danych
String wyswietl = "Select * FROM public.\"Klient\"";
Connection conn = null;
try {
//Ustawiamy dane dotyczące podłączenia
conn = DriverManager.getConnection(polaczenieURL);
//Ustawiamy sterownik MySQL
Class.forName("com.mysql.jdbc.Driver");
//Uruchamiamy zapytanie do bazy danych
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(wyswietl);
while (rs.next()) {
wyswietl(rs);
}
conn.close();
}
//Wyrzuć wyjątki jężeli nastąpią błędy z podłączeniem do bazy danych lub blędy zapytania o dane
catch(ClassNotFoundException wyjatek) {
System.out.println("Problem ze sterownikiem");
}
catch(SQLException wyjatek) {
// e.printStackTrace();
// System.out.println("Problem z logowaniem\nProsze sprawdzic:\n nazwę użytkownika, hasło, nazwę bazy danych lub adres IP serwera");
System.out.println("SQLException: " + wyjatek.getMessage());
System.out.println("SQLState: " + wyjatek.getSQLState());
System.out.println("VendorError: " + wyjatek.getErrorCode());
}
}
static void wyswietl(ResultSet rs){
try{
daneZBazy = rs.getString(1);
System.out.print("\n" + daneZBazy + " ");
daneZBazy = rs.getString(2);
System.out.print(daneZBazy + " ");
daneZBazy = rs.getString(3);
System.out.print(daneZBazy+ " ");
daneZBazy = rs.getString(4);
System.out.print(daneZBazy+ " ");
daneZBazy = rs.getString(5);
System.out.print(daneZBazy+ " ");
daneZBazy = rs.getString(6);
System.out.print(daneZBazy+ " ");
}catch(SQLException e) {
e.printStackTrace();
}
}
}
GUI:
/*
* 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.
*/
package zakladpogrzebowy;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Rafał
*/
public class GUI extends javax.swing.JFrame {
/**
* Creates new form GUI
*/
public GUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTabela = new javax.swing.JTable();
jId_klienta = new javax.swing.JTextField();
jImie = new javax.swing.JTextField();
jNazwisko = new javax.swing.JTextField();
jAdres = new javax.swing.JTextField();
jMiasto = new javax.swing.JTextField();
jTelefon = new javax.swing.JTextField();
jDodaj = new javax.swing.JButton();
jWyswietl = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTabela.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Id klienta", "Imie", "Nazwisko", "Adres", "Miasto", "Telefon"
}
));
jScrollPane1.setViewportView(jTabela);
jId_klienta.setText("jTextField1");
jImie.setText("jTextField1");
jNazwisko.setText("jTextField1");
jAdres.setText("jTextField1");
jMiasto.setText("jTextField1");
jTelefon.setText("jTextField1");
jDodaj.setText("Dodaj");
jDodaj.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jDodajActionPerformed(evt);
}
});
jWyswietl.setText("Wyświetl");
jWyswietl.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jWyswietlActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jId_klienta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jImie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jNazwisko, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jAdres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jMiasto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTelefon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jDodaj)
.addGap(18, 18, 18)
.addComponent(jWyswietl)))
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(81, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jDodaj)
.addComponent(jWyswietl))
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jId_klienta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jImie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jNazwisko, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jAdres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jMiasto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTelefon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jDodajActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model =(DefaultTableModel)jTabela.getModel();
model.addRow(new Object[]{jId_klienta.getText(),jImie.getText(),jNazwisko.getText(),jAdres.getText(),jMiasto.getText(),jTelefon.getText()});
// TODO add your handling code here:
}
private void jWyswietlActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField jAdres;
private javax.swing.JButton jDodaj;
public javax.swing.JTextField jId_klienta;
private javax.swing.JTextField jImie;
private javax.swing.JTextField jMiasto;
private javax.swing.JTextField jNazwisko;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTabela;
private javax.swing.JTextField jTelefon;
private javax.swing.JButton jWyswietl;
// End of variables declaration
}