Source Code : Update Clob data stored in MySql from a Servlet
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
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 response) throws 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 fileContent) throws 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 urlAsString) throws 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();
}
}
}
Thank with us