Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT Need source code
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Terrance
Need source code
2 Answer(s)      4 years and 10 months ago
Posted in : Swing AWT


Hai, In java swing, How can upload and retrieve the images from the mysql database?

Advertisement
View Answers

January 18, 2010 at 4:04 PM


Hi Friend,

To upload and insert image in database, try the following code:

import java.sql.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.ImageIO;

public class InsertImage {
static JTextField text=new JTextField(20);
static JButton button=new JButton("Save");
static JPanel panel=new JPanel();

public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
final File file ;
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
final String name=file.getName();
System.out.println(name);
text.setText(name);
panel.add(text);
panel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
FileInputStream fis;
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st = con.prepareStatement("insert into file(file,file_data) values(?,?)");
fis = new FileInputStream(file);
st.setString(1,name);
st.setBinaryStream(2, (InputStream)fis, (int)(file.length()));
int s = st.executeUpdate();
JOptionPane.showMessageDialog(null,"image is successfully inserted.");
}
catch(Exception ex){}
}
});
JFrame f=new JFrame("Insert Image");
f.add(panel);
f.setVisible(true);
f.setSize(300,100);
}
}
}


January 18, 2010 at 4:06 PM


continue..

To retrieve image, try the following :

import java.sql.*;
import java.awt.*;
import javax.swing.*;

public class RetrieveImage {
public static void main(String argv[]) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test";, "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select image from image where image_id='3'");
byte[] bytes = null;
if (rs.next()) {
bytes = rs.getBytes(1);
}
rs.close();
stmt.close();
con.close();
if (bytes != null) {
JFrame f = new JFrame();
f.setTitle("Display Image From database");
Image image = f.getToolkit().createImage(bytes);
f.getContentPane().add(new Pane(image));
f.setSize(300, 100);
f.setVisible(true);
}
} catch (Exception e) {
}
}
}

class Pane extends JDesktopPane {
private Image image;

public Pane(Image image) {
this.image = image;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}

Thanks


Related Tutorials/Questions & Answers:
Need source code - Swing AWT
Need source code  Hai, I need a source code for developing the project, title is "Face Recognition" and the backend as My-sql.... Thanks & Regards
Need source code - Swing AWT
Need source code  Hai, I need a idea and code for developing the project "Face Recognition" with the backend as My-sql.... Thanks & Regards
Advertisements
Need Java Source Code - JDBC
Need Java Source Code  I have a textfield for EmployeeId in which the Id, for eg: "E001" has to be generated automatically when ever i click... be implemented.  Hi friend, Please send me code because your posted
Need source code - Swing AWT
Need source code  Hai friends, How can I, view all the datas from the mysql database table in the jframe........  Hi Friend, Try the following code: import java.awt.*; import java.sql.*; import
Need source code - Swing AWT
Need source code  Hai, In java swing, How can upload and retrieve the images from the mysql database?   Hi Friend, To upload and insert image in database, try the following code: import java.sql.*; import
i need the source code to generate id in jsp
i need the source code to generate id in jsp  hai,i need the source code for generate id...which is i need to generate the new id from the maximum... of generate ids each should have some range...plz give the source code in jsp with ms
in need od source code - Development process
in need od source code  i am in the process of developing..., module, activity, and no of hours worked from week start to weekend... i need a source code for creating the front end, and for database, and linking from front end
i need attendce management system source code in java
i need attendce management system source code in java  i need attendance management system source code in java
source code
source code  sir...i need an online shopping web application on struts and jdbc....could u pls send me the source code
source code
source code  sir...i need an online shopping web application on struts and jdbc....could u pls send me the source code
source code
source code  hellow!i am developing a web portal in which i need to set dialy,weekly,monthly reminers so please give me source code in jsp
Source code
Source code  source code of html to create a user login page
need source code fir automatic timesheet filling. can anyone peass help?
need source code fir automatic timesheet filling. can anyone peass help?  Hi friends, I wants to develop a software that should automatically fills the time sheet using the login and log out entries. Can anyone please help me
source code
source code  how to use nested for to print char in java language?   Hello Friend, Try the following code: class UseNestedLoops{ public static void main(String[] args){ char[][] letters = { {'A', 'B'}, {'C','D
source code
source code  how to get the fields from one page to another page in struts by using JSTL
need of code
need of code  howto convert greyscale image to binary image in java
need code
need code  Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck which has the following additional... is the required code: import java.util.*; class Vehicle { int
need code
need code  Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck which has the following additional... is the required code: import java.util.*; class Vehicle { int
need code
need code  Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck which has the following additional... is the required code: import java.util.*; class Vehicle { int
source code in jsp
source code in jsp  i need the source code for online payment in jsp
request for java source code
request for java source code  I need source code for graphical password using cued-click points enabled with sound signature in java and oracle 9i as soon as possible... Plz send to my mail
source code in jsp
source code in jsp  sir...i need the code for inserting images into the product catalog page and code to display it to the customers when they login to site..pls help me
source code - JSP-Servlet
source code  i need source code for client server application in jsp to access multiple clients   Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/ Here you will get lot of examples
Need a JSP CODE
Need a JSP CODE  *Hi I need JSP source code for selling products which are in database.. please can any one help me ..URGENT* Thank You
source code for the following question
source code for the following question  source code for the fuzzy c-means
Java source code
Java source code  How are Java source code files named
java source code
java source code  java source code to create mail server using struts2
java/j2ee source code
java/j2ee source code  java/j2ee source code for online food purchasing service with my sql database
Hiding Source Code - WebSevices
Hiding Source Code  Is there any way to prevent viewing source code in browser
source code - Swing AWT
source code  source code for a program to shutdown, restart, log off the pc after clicking jbutton on jpanel or a jframe by user. thanks in advance
source code security
source code security  how to obfuscate the code? please answer as soon as possible
Advertisements
 

 

 

DMCA.com