如何在Java中将字符串转换为Blob? [英] How to convert a String to blob in java?

查看:95
本文介绍了如何在Java中将字符串转换为Blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大邮件正文的字符串.我需要将它以Blob的形式保存到数据库中.如何在Java中将其转换为?下面的示例字符串:

I have a String which contains a large mail body. I need to save it to the database in the form of blob. How to convert it to the blob in java? Sample String below:

<html><center> <body style='background-color: #e5e4e2;'> <table id='mainTable' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'><tr> <td> <table style='width: 100%;background-color: #2B547E; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'> <tr> <td width='50%'><img src="cid:image" height='50' width='150' style='padding-left: 10px;'></td> <td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td></tr> </table> </td> </tr></table> <table id='mainTable1' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> <tr> <td> <table style='width: 100%; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'><tr> <td width='50%' style='color:red;font-size:21px;'>Congratulations, Joselyn Valdes-Flores </td> <td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td> </tr> <tr> <td style='font-size:18px;width:97%;font-style:italic'> This email confirms your Phone Interview  with Manager, Accounts Receivable  in   Burger King Corporation  You will be called at your mobile number </td> </tr> </table> </td> </tr> </table> <table  style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'><tr style='color:green'> Interview LineUp <th style='color:gray;font-size:21px;text-align:left;font-style: italic'>Interviewe</th><th  style='color:gray;font-size:21px;text-align:left;font-style: italic'>Timing1</th><th  style='color:gray;font-size:21px;text-align:left;font-style: italic'>Timing2</th></tr><tr><td>Jeet Chatterjee</td><td>12:00am</td><td>2:00am</td></tr><tr><td>Ramu</td><td>2:00am</td><td>4:00am</td></tr></table><table id='mainTable2' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto; margin-top:12px;'><tr> <td style='font-size:18px; color:#2B547E;word-wrap: break-word;font-style: italic'> hii</td> </tr> </table> <table  style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> <tr> <td style='font-size:18px;color:#2B547E;word-wrap: break-word;font-style: italic'> Best Regards,</td><td style='font-size:24px;color:#2B547E;word-wrap: break-word;font-style: italic'> Jeet Chatterjee</td></tr> </table></body> </center> </html> 

我想将其保存在blob列中的数据库中.怎么做??

I want to save it in a database in the blob column. How to do this??

推荐答案

您可以通过从 String

byte[] byteData = yourString.getBytes("UTF-8");//Better to specify encoding
Blob blobData = dbConnection.createBlob();
blobData.setBytes(1, byteData);

此外,您可以将 String 直接存储到 BLOB 列中.

Moreover, you can directly store String to BLOB column.

这篇关于如何在Java中将字符串转换为Blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆