JAVA CODE TO STORE IMAGE IN DATABASE

import java.sql.*;
import java.io.*;

class MySqlSaveImage
{
public static void main(String[] rk) throws SQLException
{

Connection connection = null;
String connectionURL = “jdbc:mysql://localhost:3306/mydb”;
ResultSet rs = null;

PreparedStatement psmnt = null;

FileInputStream fis;

try {

Class.forName(“com.mysql.jdbc.Driver”);//.newInstance();
connection = DriverManager.getConnection(connectionURL, “username”, “password”);
File image = new File(“c:/Me.jpg”);

psmnt = connection.prepareStatement(“insert into images(image) “+ “values(?)”);

fis = new FileInputStream(image);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));

int flag= psmnt.executeUpdate();
if(flag>0)
{
System.out.println(“Uploaded successfully !”);
}
else
{
System.out.println(“unsucessfull to upload image.”);
}
}
catch (Exception ex)
{
System.out.println(“Found some error : “+ex);
}
finally
{
connection.close();
psmnt.close();
}
}
}

Friend leave your comments. Your comments are heartly invited

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s