Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:24:57+00:00 2026-05-31T23:24:57+00:00

I need some guidance around which approach to use to load binary files from

  • 0

I need some guidance around which approach to use to load binary files from a folder into a MySQL Database using Camel. Basically I want to store voice logs from our PBX system into a database. The directory with the voice logs will be a remote directory

I have designed a prototype but I am not sure if this is really efficient, it works but I am not happy with the design. Let me explain what I am doing. Camel route as follows:

    <camelContext xmlns="http://camel.apache.org/schema/spring">
    <package>com.hia.camelone</package>
      <route>
            <from uri="file://c:/CTest/Inbox?noop=true&amp;recursive=true&amp;delay=3000"/>
            <to uri="bean://fileToSQL"/>
            <to uri="jdbc://timlogdb"/>

       </route>

</camelContext>

<bean id="timlogdb" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value=" com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/TimLog" />
    <property name="username" value="root" />
    <property name="password" value="blahblah" />
</bean>
<bean id="fileToSQL" class="com.hia.camelone.fileToSQL"/>

And the code to fileToSQL bean is:

public class fileToSQL {

public String toString(@Headers Map<String,Object> header, @Body Object body){
    StringBuilder sb = new StringBuilder();
    String filename =(String)header.get("CamelFileNameOnly");
    String escapedFileName = StringEscapeUtils.escapeJava(filename).replace("\'", "");
    String filePath = StringEscapeUtils.escapeJava((String)header.get("CamelFilePath"));

    sb.append("insert into FileLog ");
    sb.append("(FileName,FileData) values (");
    sb.append("'").append(escapedFileName).append("',").append("LOAD_FILE(\"").append(filePath).append("\")");
    sb.append(")");
    System.out.println(sb.toString());
    System.out.println(body);
    System.out.println(header.toString());
    return sb.toString();
}
}

Ok short explanation I get the file component to consume the files then I build a SQL string using the MySQL LOAD_FILE() function to load the file.

My thoughts around this:

The LOAD_FILE function only works on the local machine and thus this route will only with the files being on the local machine. I could use a file producer to copy the files from some remote directory to a local directory and then use the route. My route would be something like this then:

<route>
            <from uri="file://c:/CTest/Inbox?noop=true&amp;recursive=true&amp;delay=3000"/>
            <to uri="file://c:/outbox"/>
            <to uri="bean://fileToSQL"/>
            <to uri="jdbc://timlogdb"/>

</route>

However since I have access to the files content in the message from the files consumer I should be able to theoretically be able to access the body/content of the string and build a SQL command that does NOT use the LOAD_FILE() function.

The only way I know how to build such a string is by using the prepared statement of JDBC. This would be first prize if I could somehow build a insert statement with the content from the file consumer.

Can I create a prepared statement in my fileToSQL bean and pass it to my jdbc component?
Or how do I build a INSERT statement without the LOAD_FILE() function?

Since I have to use the LOAD_FILE() function I would now have to cater for both unix and windows filepaths. While this should not be difficult I just dont like the idea of putting OS specific code into my applications(feels like a work around).

Anybody here ever uploaded binary files to a MySQL database using Camel who can give me some guidance on the points above. While I could work around the problems I just want to make sure I dont miss a obvious way of doing things.

I had a look around here and only found people working with mostly text files. Guys please don’t even go down the route of me storing the file on the files system and linking it to the database. We have some very specific disaster recovery requirements and legal requirements that enforce the need for me to store it in a database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:24:58+00:00Added an answer on May 31, 2026 at 11:24 pm

    Right so I managed to find a way and it was not that difficult. What I essentially did was get rid of the JDBC Camel Component in the route. I then injected the data source bean into my fileToSQL bean. I then used a simple prepared statement to insert the file and its name into MySQL.

    As always code is much more explicit than my english.

     <camelContext xmlns="http://camel.apache.org/schema/spring">
        <package>com.hia.camelone</package>
    
          <route>
                <from uri="file://c:/CTest/Inbox?noop=true&amp;recursive=true&amp;delay=3000"/>
                <to uri="bean://fileToSQL"/>
                <!--<to uri="jdbc://timlogdb"/>-->
    
           </route>
    
    </camelContext>
    
    <bean id="timlogdb" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value=" com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/TimLog" />
        <property name="username" value="root" />
        <property name="password" value="lalala" />
    </bean>
    <bean id="fileToSQL" class="com.hia.camelone.fileToSQL">
        <property name="dataSource" ref="timlogdb"/>
    </bean>
    

    As you can see I inject my timlogdb bean into my fileToSQL bean. Spring ROCKS!

    So here is my fileToSQL bean.

    public class fileToSQL {
    private DriverManagerDataSource dataSource;
    private static final String SQL_INSERT="insert into FileLog(FileName,FileData)values(?,?)";
    @Handler
    public void toString(@Headers Map<String,Object> header,Exchange exchange){
        Connection conn = null;
        PreparedStatement stmt=null;
        String filename =StringEscapeUtils.escapeJava(((String)header.get("CamelFileNameOnly")).replace("\'", ""));
    
        try {
            conn= dataSource.getConnection();
            stmt =conn.prepareStatement(SQL_INSERT);
            stmt.setString(1, filename);
            byte[] filedata = exchange.getIn().getBody(byte[].class);
            stmt.setBytes(2,filedata );
            int s = stmt.executeUpdate();
    
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        finally{
            try
            {
                    if (stmt!=null)
                    {
                        stmt.close();
                    }
                    if (conn!=null)
                    {
                        conn.close();
                    }
            }
            catch(SQLException e)
            {
                System.out.println(e.getMessage());
            }
        }
    
    
    }
    
    /**
     * @param dataSource the dataSource to set
     */
    public void setDataSource(DriverManagerDataSource dataSource) {
        this.dataSource = dataSource;
    }
    }
    

    The guys from Camel did a great job. Camel is truly flexible especially when you combine it with Spring.

    What a ride!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some guidance in getting value from the database within a week time
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for
I am new in this field and i desperately need some guidance from u
Really need just some guidance : Topological sort by arcs definition (from my question)
I'm new to PHP, so I need some guidance as to which would be
I need some guidance on how to write a query in Ms-Access which will
I need some guidance/advices from someone who has any ideea about custom jquery event.
I need some guidance/tutorial links/code logic to know how can we use web to
Im new to database design and need some guidance. A lot of new data
I am diving into the web application testing line(php ,ajax,javascript) and need some guidance.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.