Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: add image to database using java

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add image to database using java

    i want to add this image to database how can i do
    please help me
    Attached Files Attached Files


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Location
    Denmark, Albertslund
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: add image to database using java

    You know how you connect to a server?

    --- Update ---

    And who database you will used?

    --- Update ---

    I think when you see this code you have a ide how you can do the :-)

    import java.sql.*;
    public class Databasekommunikation
    {
    public static void main(String[] arg) throws Exception
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("Driver indlęst");

    Connection forb = DriverManager.getConnection(
    "jdbcracle:thin:@ora.javabog.dk:1521:student","jacob"," jacob");
    System.out.println("Forbindelse oprettet");

    Statement stmt = forb.createStatement();

    try { stmt.executeUpdate("drop table KUNDER"); } catch (Exception e) {}

    stmt.executeUpdate("create table KUNDER (NAVN varchar(32), KREDIT number)");
    System.out.println("Tabel oprettet");

    stmt.executeUpdate("insert into KUNDER values('Jacob', -1799)");

    stmt.executeUpdate("insert into KUNDER(NAVN,KREDIT) values('Brian', 0)");

    String navn = "Hans";
    double kredit = 500;
    stmt.executeUpdate(
    "insert into KUNDER(NAVN,KREDIT) values('"+navn+"', "+kredit+")");

    ResultSet rs = stmt.executeQuery("select NAME, CREDIT from KUNDER");
    while (rs.next())
    {
    name = rs.getString("NAME");
    kredit = rs.getDouble("KREDIT");
    System.out.println(navn+" "+kredit);
    }
    }
    }


    Driver indlęst
    Forbindelse oprettet
    Tabel oprettet
    Jacob -1799.0
    Brian 0.0
    Hans 500.0

    Example how you can send things to you database. Here was the database MySql :-)

    Hope you can now. Or write here... You shall not see on they words you not understand just look on the programming.
    Last edited by magnus110498; March 18th, 2014 at 10:39 AM.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    Hong Kong
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: add image to database using java

    Here is a basic example for adding image to database using java.
    public static void main(String[] args) throws Exception {
    // Select file
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILE S_ONLY);
    fileChooser.showOpenDialog(null);
    File selectedFile = fileChooser.getSelectedFile();

    // Insert image
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(
    "jdbc:mysql://localhost/databaseName", "root", "root");
    String INSERT_PICTURE = "insert into MyPictures(photo) values (?)";
    conn.setAutoCommit(false);
    FileInputStream fis = new FileInputStream(selectedFile);
    PreparedStatement ps = conn.prepareStatement(INSERT_PICTURE);
    ps.setBinaryStream(1, fis, (int) selectedFile.length());
    ps.executeUpdate();
    conn.commit();
    ps.close();
    fis.close();
    conn.close();

    // Show image
    BufferedImage bufferedImage = ImageIO.read(selectedFile);
    ImageIcon imageIcon = new ImageIcon(bufferedImage);
    JLabel label = new JLabel();
    label.setIcon(imageIcon);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add(label);
    frame.pack();
    frame.setVisible(true);
    }

Similar Threads

  1. Add an object to a linked list and a database
    By a21j92 in forum JDBC & Databases
    Replies: 0
    Last Post: March 2nd, 2013, 09:09 AM
  2. How to add Image to my JButtons?
    By sim18 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: November 26th, 2012, 01:35 PM
  3. How can i add an image into my source code?
    By joelmeler in forum Java Theory & Questions
    Replies: 14
    Last Post: August 1st, 2011, 06:15 PM
  4. Add Image to JPanel
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 16th, 2011, 02:44 PM
  5. image and sounds mp3 database
    By Stefan_Lam in forum JDBC & Databases
    Replies: 0
    Last Post: May 7th, 2010, 05:30 AM