Commit 5feeadb8 by Patryk Czarnik

Przykłady BLOB

parent df6c0198
......@@ -25,7 +25,10 @@ public class P02_Odczyt_Try {
Date date = rs.getDate("hire_date");
LocalDate localDate = date.toLocalDate();
System.out.printf("%d %s %s %s %s %s%n", id, firstName, lastName, jobId, salary, date);
// numer wiersza
int nr = rs.getRow();
System.out.printf("%3d. %d %s %s %s %s %s%n", nr, id, firstName, lastName, jobId, salary, date);
}
} catch (SQLException e) {
e.printStackTrace();
......
......@@ -9,7 +9,7 @@ import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class P07_GenerowaneId {
/* W abzach danych często stosowane są automatycznie generowane klucze. Gdy wykomnujemy polecenie INSERT nie wymieniając kolumny z kluczem,
/* W bazach danych często stosowane są automatycznie generowane klucze. Gdy wykonujemy polecenie INSERT nie wymieniając kolumny z kluczem,
* jego wartość jest generowana automatycznie (z sekwencji lub w oparciu o specjalny typ kolumny).
* Ale aplikacja zwykle potrzebuje poznać to ID, aby móc odwoływać się do tego rekordu w dalszej części programu.
* JDBC oferuje mechanizm, w którym przed wykonaniem operacji INSERT w obiekcie Statement rejestruje się automatycznie generowane kolumny,
......
......@@ -19,6 +19,7 @@ public class P08a_ModyfikacjaPoprzezWynik {
stmt.setInt(1, limit);
try(ResultSet rs = stmt.executeQuery()) {
System.out.println("Nazwa kursora: " + rs.getCursorName());
while(rs.next()) {
try {
Thread.sleep(2000);
......
package gotowe.postgresql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class P13a_Blob_Create {
public static void main(String[] args) {
final String sql = """
DROP TABLE IF EXISTS photos;
CREATE TABLE photos(
photo_id SERIAL PRIMARY KEY,
file_name VARCHAR(60) NOT NULL,
bytes BYTEA NOT NULL
)
""";
try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD);
Statement stmt = c.createStatement()) {
stmt.execute(sql);
System.out.println("Gotowe");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package gotowe.postgresql;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class P13b_Blob_Insert {
public static void main(String[] args) {
String[] polaGenerowane = {"photo_id"};
String sql = "INSERT INTO photos(file_name, bytes) VALUES(?, ?)";
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.setDialogTitle("Wybierz plik");
fileChooser.setFileFilter(new FileNameExtensionFilter("Pliki graficzne", "jpg", "jpeg", "png", "bmp"));
try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD)) {
// org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode
c.setAutoCommit(false);
try(PreparedStatement stmt = c.prepareStatement(sql, polaGenerowane)) {
while(true) {
int wybor = fileChooser.showOpenDialog(null);
if(wybor == JFileChooser.CANCEL_OPTION) break;
if(wybor == JFileChooser.ERROR_OPTION) continue;
File selectedFile = fileChooser.getSelectedFile();
try(InputStream input = new FileInputStream(selectedFile)) {
stmt.setString(1, selectedFile.getName());
//stmt.setBlob(2, input);
stmt.setBinaryStream(2, input);
int ile = stmt.executeUpdate();
try(ResultSet generatedKeys = stmt.getGeneratedKeys()) {
if(ile == 1 && generatedKeys.next()) {
int id = generatedKeys.getInt(1);
JOptionPane.showMessageDialog(null, "Dodano zdjęcie " + selectedFile.getName() + " pod numerem " + id);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
int wybor = JOptionPane.showConfirmDialog(null, "Czy zatwierdzasz zmiany?");
switch(wybor) {
case JOptionPane.YES_OPTION -> c.commit();
case JOptionPane.NO_OPTION -> c.rollback();
}
JOptionPane.showMessageDialog(null, "Gotowe");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package gotowe.postgresql;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class P13c_Blob_Download {
public static void main(String[] args) {
String sql = "SELECT photo_id, file_name, bytes FROM photos ORDER BY 1";
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.setFileFilter(new FileNameExtensionFilter("Pliki graficzne", "jpg", "jpeg", "png", "bmp"));
try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD)) {
// org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode
c.setAutoCommit(false);
c.setReadOnly(true);
try(PreparedStatement stmt = c.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
int id = rs.getInt(1);
String fileName = rs.getString(2);
fileChooser.setDialogTitle("Zapisz plik nr " + id);
fileChooser.setSelectedFile(new File(fileName));
int wybor = fileChooser.showSaveDialog(null);
if(wybor != JFileChooser.APPROVE_OPTION) continue;
File selectedFile = fileChooser.getSelectedFile();
try(InputStream input = rs.getBinaryStream(3);
OutputStream output = new FileOutputStream(selectedFile)) {
input.transferTo(output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
JOptionPane.showMessageDialog(null, "Gotowe");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package gotowe.postgresql;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class P13d_Blob_Download {
public static void main(String[] args) {
String sql = "SELECT photo_id, file_name, bytes FROM photos ORDER BY 1";
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.setFileFilter(new FileNameExtensionFilter("Pliki graficzne", "jpg", "jpeg", "png", "bmp"));
try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD)) {
// org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode
c.setAutoCommit(false);
c.setReadOnly(true);
try(PreparedStatement stmt = c.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
int id = rs.getInt(1);
String fileName = rs.getString(2);
Blob blob = rs.getBlob(3);
try {
fileChooser.setDialogTitle("Zapisz plik nr " + id);
fileChooser.setSelectedFile(new File(fileName));
int wybor = fileChooser.showSaveDialog(null);
if(wybor != JFileChooser.APPROVE_OPTION) continue;
File selectedFile = fileChooser.getSelectedFile();
try(InputStream input = blob.getBinaryStream();
OutputStream output = new FileOutputStream(selectedFile)) {
input.transferTo(output);
} catch (IOException e) {
e.printStackTrace();
}
} finally {
blob.free();
}
}
}
JOptionPane.showMessageDialog(null, "Gotowe");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package gotowe.postgresql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class P13e_Blob_Show {
public static void main(String[] args) {
String sql = "SELECT photo_id, file_name, bytes FROM photos ORDER BY 1";
try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD)) {
c.setAutoCommit(false);
c.setReadOnly(true);
try(PreparedStatement stmt = c.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
int id = rs.getInt(1);
String fileName = rs.getString(2);
byte[] bytes = rs.getBytes(3);
ImageIcon img = new ImageIcon(bytes);
JOptionPane.showMessageDialog(null, img, "Zdjęcie nr " + id, JOptionPane.INFORMATION_MESSAGE);
}
}
JOptionPane.showMessageDialog(null, "Gotowe");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment