Results 1 to 3 of 3
  1. #1
    thamizhisai is offline Member
    Join Date
    Apr 2008
    Posts
    16
    Rep Power
    0

    Default java with database connectivity

    i want to insert images using Mysql database and Java as my programming language to connect to the database

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello Dear,


    check this sample code..

    Java 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();
    	}
    }
    sanjeev,संजीव

  3. #3
    mmahesh_mca is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default creating image in java

    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

Similar Threads

  1. database connectivity
    By rilwanfit in forum Networking
    Replies: 1
    Last Post: 03-13-2009, 07:54 PM
  2. java database connectivity
    By thamizhisai in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2008, 05:59 PM
  3. ensure connectivity
    By perdoname in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 02:29 AM
  4. Database connectivity in Jave struts with Hibernate
    By sanjay_sharma77 in forum Advanced Java
    Replies: 1
    Last Post: 02-02-2008, 08:41 PM
  5. Java Database Connectivity
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 01-14-2008, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •