//before executing this program you have to create table
create table personaldetails (
empno int(3) not null default '0',
photo BLOB(1000) NULL,
permanent_address varchar(25) not null default '',
present_address varchar(25) not null default '',
);
2) here i have kept one photo graph that photo file name is raj with gif extension in D:\jdbc\prog\wed\Blob\raj.gif file
*/
import java.sql.*;
import java.io.*;
class SaveImageToDatabase {
public static void main(String[] args) throws SQLException {
// declare a connection by using Connection interface
Connection connection = null;
/* Create string of connection url within specified format with machine
name, port number and database name. Here machine name id localhost
and database name is test. */
String connectionURL = "jdbc:mysql://localhost:3306/test";
/*declare a resultSet that works as a table resulted by execute a specified
sql query. */
ResultSet rs = null;
// Declare prepare statement.
PreparedStatement psmnt = null;
// declare FileInputStream object to store binary stream of given image.
FileInputStream fis;
try {
// Load JDBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes
parameters of string type connection url, user name and password to
connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "admin");
// create a file object for image by specifying full path of image as parameter.
//File image = new File("C:/image.jpg");
File image = new File("D:/jdbc/prog/wed/Blob/raj.GIF");
/* prepareStatement() is used for create statement object that is
used for sending sql statements to the specified database. */
psmnt = connection.prepareStatement
//("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)");
("insert into personaldetails(empno,photo,permanent_address)"+ "values(?,?,?)");
psmnt.setInt(1,3);
fis = new FileInputStream(image);
psmnt.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
psmnt.setString(3,"Hyd");
//psmnt.setString(4,"123456");
//fis = new FileInputStream(image);
//psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query
insert data and image from specified address. */
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else {
System.out.println("unsucessfull to upload image.");
}
}
// catch if found any exception during rum time.
catch (Exception ex) {
System.out.println("Found some error : "+ex);
}
finally {
// close all the connections.
connection.close();
psmnt.close();
}
}
}
create table personaldetails (
empno int(3) not null default '0',
photo BLOB(1000) NULL,
permanent_address varchar(25) not null default '',
present_address varchar(25) not null default '',
);
2) here i have kept one photo graph that photo file name is raj with gif extension in D:\jdbc\prog\wed\Blob\raj.gif file
*/
import java.sql.*;
import java.io.*;
class SaveImageToDatabase {
public static void main(String[] args) throws SQLException {
// declare a connection by using Connection interface
Connection connection = null;
/* Create string of connection url within specified format with machine
name, port number and database name. Here machine name id localhost
and database name is test. */
String connectionURL = "jdbc:mysql://localhost:3306/test";
/*declare a resultSet that works as a table resulted by execute a specified
sql query. */
ResultSet rs = null;
// Declare prepare statement.
PreparedStatement psmnt = null;
// declare FileInputStream object to store binary stream of given image.
FileInputStream fis;
try {
// Load JDBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes
parameters of string type connection url, user name and password to
connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "admin");
// create a file object for image by specifying full path of image as parameter.
//File image = new File("C:/image.jpg");
File image = new File("D:/jdbc/prog/wed/Blob/raj.GIF");
/* prepareStatement() is used for create statement object that is
used for sending sql statements to the specified database. */
psmnt = connection.prepareStatement
//("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)");
("insert into personaldetails(empno,photo,permanent_address)"+ "values(?,?,?)");
psmnt.setInt(1,3);
fis = new FileInputStream(image);
psmnt.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
psmnt.setString(3,"Hyd");
//psmnt.setString(4,"123456");
//fis = new FileInputStream(image);
//psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query
insert data and image from specified address. */
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else {
System.out.println("unsucessfull to upload image.");
}
}
// catch if found any exception during rum time.
catch (Exception ex) {
System.out.println("Found some error : "+ex);
}
finally {
// close all the connections.
connection.close();
psmnt.close();
}
}
}
No comments:
Post a Comment