Update Clob data stored in MySql from a Servlet : Database « Servlets « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Servlets » DatabaseScreenshots 
Update Clob data stored in MySql from a Servlet

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateMySqlClobServlet extends HttpServlet {

  public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/databaseName";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public void doGet(HttpServletRequest request, HttpServletResponse responsethrows IOException,
      ServletException {
    Connection conn = null;
    String id = "001";
    String fileAsURL = "http://yourwebsite/fileName.dat";
    ServletOutputStream out = response.getOutputStream();
    response.setContentType("text/html");
    out.println("<html><head><title>UpdateMySqlClobServlet</title></head>");

    try {
      conn = getConnection();
      String fileContent = getClobsContentAsString(fileAsURL);
      updateCLOB(conn, id, fileContent);
      out.println("<body><h4>OK: updated an existing record with id=" + id + "</h4></body></html>");
    catch (Exception e) {
      e.printStackTrace();
      out.println("<body><h4>Error: " + e.getMessage() "</h4></body></html>");
    }
  }
  public void updateCLOB(Connection conn, String id, String fileContentthrows Exception {
    PreparedStatement pstmt = null;
    try {
      pstmt = conn.prepareStatement("update dataTable set filebody= ? where id = ?");
      pstmt.setString(1, fileContent);
      pstmt.setString(2, id);
      pstmt.executeUpdate();
    finally {
      pstmt.close();
    }
  }

  public static String getClobsContentAsString(String urlAsStringthrows Exception {
    InputStream content = null;
    try {
      java.net.URL url = new java.net.URL(urlAsString);
      java.net.URLConnection urlConn = url.openConnection();
      urlConn.connect();
      content = urlConn.getInputStream();

      int BUFFER_SIZE = 1024;
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      int length;
      byte[] buffer = new byte[BUFFER_SIZE];

      while ((length = content.read(buffer)) != -1) {
        output.write(buffer, 0, length);
      }
      return new String(output.toByteArray());

    finally {
      content.close();
    }
  }

}
           
       
Related examples in the same category
1. Servlets Database Query
2. Using JDBC in Servlets
3. Cached Connection Servlet
4. Transaction Connection Servlet
5. Session Login JDBC
6. JDBC and Servlet
7. Database and Servlet: Database MetaData
8. Database and Servlet: Store procedure
9. Database transaction
10. Typical database commands
11. Process a raw SQL query; use ResultSetMetaData to format it
12. See Account
13. Guest Book Servlet
14. Dedicated Connection Servlet
15. Login Servlets
16. OCCI Connection Servlet
17. Get Column Names From ResultSet
18. Display Clob Servlet
19. Delete Blob From Servlet
20. Delete Clob From Servlet
21. Display Blob Servlet
22. Delete Clob From Oracle in a Servlet
23. Insert Clob to MySql Servlet
w__w__w___.__j__a__va___2___s___.___co_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.