i want to insert images using Mysql database and Java as my programming language to connect to the database
Printable View
i want to insert images using Mysql database and Java as my programming language to connect to the database
Hello Dear,
check this sample code..
Code:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import javax.imageio.ImageIO;
import java.sql.*;
import javax.swing.ImageIcon;
public class test
{ public test()
{ System.out.println("Executing");
try
{ ImageIcon icon = new ImageIcon("c:/a.jpg");
String empID="aravind";
Image image = icon.getImage();
BufferedImage bImage = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image,0,0,null);
bg.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage,"jpeg",out);
byte[] buf = out.toByteArray();
// setup stream for blob
ByteArrayInputStream inStream = new ByteArrayInputStream(buf);
// get or create a connection here
try
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(ClassNotFoundException e)
{ System.out.println(e);
}
String dbURL = "jdbc:mysql:///test";
Connection dbCon;
dbCon = DriverManager.getConnection(dbURL,"root","root");
//Connection photoConnection = startup.connectionPool.getConnection();
String query = "insert into employeephoto (Employee_ID,Binary_Photo,LastUser,LastMod,Created) values ('"+empID+"', ?, 'chandu', NOW(), NOW())";
PreparedStatement ps = dbCon.prepareStatement(query);
ps.setBinaryStream(1,inStream,inStream.available());
int n = ps.executeUpdate();
System.out.println(n);
// release or close connection here
dbCon.close();
//startup.connectionPool.releaseConnection(photoConnection);
}
catch(SQLException e)
{ System.out.println(e);
}
}
catch (Exception exc)
{ // process error
System.out.println("process error" + exc);
}
}
public static void main(String args[])
{ System.out.println("Main Executing");
test t = new test();
}
}
i want to view a image(that image having only alphabets) in webpage below that image i need a link as "refresh the image" (once i click this link means the image autimatically changes),below that textbox and submit button. if i entered text in the textbox as same text on the image means it show another page u r image and text are matched if else not matched. And i dont want to use any database for this