guardarimagenenbasedatos_en_jsp.pdf

1
SaveImageToDatabase.java — Printed on 30/07/2008, 9:20:48 — Page 1 1 2 import java.sql.*; 3 import java.io.*; 4 5 class SaveImageToDatabase { 6 public static void main(String[] args) throws SQLException { 7 8 // declare a connection by using Connection interface 9 Connection connection = null; 10 11 /* Create string of connection url within specified format with machine 12 name, port number and database name. Here machine name id localhost 13 and database name is mahendra. */ 14 15 String connectionURL = "jdbc:mysql://localhost:3306/mahendra"; 16 17 /*declare a resultSet that works as a table resulted by execute a specified 18 sql query. */ 19 ResultSet rs = null; 20 21 // Declare statement. 22 PreparedStatement psmnt = null; 23 24 // declare FileInputStream object to store binary stream of given image. 25 FileInputStream fis; 26 27 try { 28 29 // Load JDBC driver "com.mysql.jdbc.Driver" 30 Class.forName("com.mysql.jdbc.Driver").newInstance(); 31 32 /* Create a connection by using getConnection() method that takes 33 parameters of string type connection url, user name and password to 34 connect to database. */ 35 connection = DriverManager.getConnection(connectionURL, "root", "root"); 36 37 // create a file object for image by specifying full path of image as parameter. 38 File image = new File("C:/image.jpg"); 39 40 /* prepareStatement() is used for create statement object that is 41 used for sending sql statements to the specified database. */ 42 psmnt = connection.prepareStatement("insert into save_image(name, city, image, Phone) "+ 43 "values(?,?,?,?)"); 44 psmnt.setString(1,"mahendra"); 45 psmnt.setString(2,"Delhi"); 46 psmnt.setString(4,"123456"); 47 48 fis = new FileInputStream(image); 49 50 psmnt.setBinaryStream(3,(InputStream)fis,(int)(image.length())); 51 52 /* executeUpdate() method execute specified sql query. Here this query 53 insert data and image from specified address. */ 54 int s = psmnt.executeUpdate(); 55 if(s>0){ 56 System.out.println("Uploaded successfully !"); 57 } 58 else { 59 System.out.println("unsucessfull to upload image."); 60 } 61 } 62 63 // catch if found any exception during rum time. 64 catch (Exception ex){ 65 System.out.println("Found some error : "+ex); 66 } 67 68 finally { 69 // close all the connections. 70 connection.close(); 71 psmnt.close(); 72 } 73 } 74 } 75 C:\SaveImageToDatabase.java — File date: 18/07/2008 — File time: 11:48:10

Upload: jose-merino

Post on 21-Jul-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

SaveImageToDatabase.java — Printed on 30/07/2008, 9:20:48 — Page 1

1 2 import java.sql.*;3 import java.io.*;4 5 class SaveImageToDatabase {6 public static void main(String[] args) throws SQLException {7 8 // declare a connection by using Connection interface 9 Connection connection = null;

10 11 /* Create string of connection url within specified format with machine 12 name, port number and database name. Here machine name id localhost 13 and database name is mahendra. */14 15 String connectionURL = "jdbc:mysql://localhost:3306/mahendra";16 17 /*declare a resultSet that works as a table resulted by execute a specified 18 sql query. */19 ResultSet rs = null;20 21 // Declare statement.22 PreparedStatement psmnt = null;23 24 // declare FileInputStream object to store binary stream of given image.25 FileInputStream fis;26 27 try {28 29 // Load JDBC driver "com.mysql.jdbc.Driver"30 Class.forName("com.mysql.jdbc.Driver").newInstance();31 32 /* Create a connection by using getConnection() method that takes 33 parameters of string type connection url, user name and password to 34 connect to database. */35 connection = DriverManager.getConnection(connectionURL, "root", "root");36 37 // create a file object for image by specifying full path of image as parameter.38 File image = new File("C:/image.jpg");39 40 /* prepareStatement() is used for create statement object that is 41 used for sending sql statements to the specified database. */42 psmnt = connection.prepareStatement("insert into save_image(name, city, image, Phone) "+43 "values(?,?,?,?)");44 psmnt.setString(1,"mahendra");45 psmnt.setString(2,"Delhi");46 psmnt.setString(4,"123456");47 48 fis = new FileInputStream(image);49 50 psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));51 52 /* executeUpdate() method execute specified sql query. Here this query 53 insert data and image from specified address. */54 int s = psmnt.executeUpdate();55 if(s>0) {56 System.out.println("Uploaded successfully !");57 }58 else {59 System.out.println("unsucessfull to upload image.");60 }61 }62 63 // catch if found any exception during rum time.64 catch (Exception ex) {65 System.out.println("Found some error : "+ex);66 }67 68 finally {69 // close all the connections.70 connection.close();71 psmnt.close();72 }73 }74 }75

C:\SaveImageToDatabase.java — File date: 18/07/2008 — File time: 11:48:10