package com.img;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class ImageTest extends javax.swing.JFrame {
/** Creates new form ImageTest */
public ImageTest() {
initComponents();
}
public ImageIcon getDbImage() {
ImageIcon dPhoto = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:4406/db_coders",
"root","321654");
Statement stmt=conn.createStatement();
// SQL e dikkat edin
String sql="select resim from tablo1 where id=3";
ResultSet res;
res=stmt.executeQuery(sql);
try {
if (res.next()) {
Blob image = res.getBlob("resim");
InputStream input = image.getBinaryStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] rb = new byte[1024]; // buffer
int ch = 0;
while ((ch=input.read(rb)) != -1){output.write(rb, 0, ch);}
byte[] b = output.toByteArray();
input.close();
output.close();
dPhoto = new ImageIcon(b);
} else {/*nullresim gönderilebilir*/}
} catch (Exception e){e.printStackTrace();}
}catch(Exception e){
System.out.println(e);
}
return dPhoto;
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
getContentPane().setLayout(new java.awt.GridLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("get ");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton2.setText("insert db");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//db ye resmi sok
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:4406/db_coders",
"root","321654");
PreparedStatement ps;
String sql=" insert into tablo1(resim) values(?)";
ps=conn.prepareStatement(sql);
File fileIn = new File("c:\sharone1.jpg");
int fileLength = (int)fileIn.length();
InputStream streamedJpg = new FileInputStream(fileIn);
ps.setBinaryStream(1,streamedJpg,fileLength);
ps.executeUpdate();
ResultSet res;
conn.close();
}catch(Exception e){
System.out.println(e);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton1.setIcon(this.getDbImage());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ImageTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
C ye sharone1.jpg diye bir resim at abi. poblem db deki data bozukmuş.
N/A
|