Java Tutorial/Database/Long Text

Материал из Java эксперт
Перейти к: навигация, поиск

Get a Long Text Column/Field from a Database

   <source lang="java">

public static String getLargerString(ResultSet rs, int columnIndex) throws SQLException {

   InputStream in = null;
   int BUFFER_SIZE = 1024;
   try {
     in = rs.getAsciiStream(columnIndex);
     if (in == null) {
       return "";
     }
     byte[] arr = new byte[BUFFER_SIZE];
     StringBuffer buffer = new StringBuffer();
     int numRead = in.read(arr);
     while (numRead != -1) {
       buffer.append(new String(arr, 0, numRead));
       numRead = in.read(arr);
     }
     return buffer.toString();
   } catch (Exception e) {
     e.printStackTrace();
     throw new SQLException(e.getMessage());
   }
 }</source>
   
  
 
  



How do you Store a Long Text Field in a Database?

   <source lang="java">

public static void setLongString(PreparedStatement pstmt, int parameterIndex, String data)

     throws Exception {
   // possibly a long string
   pstmt.setAsciiStream(parameterIndex, new ByteArrayInputStream(data.getBytes()), data.length());
 }</source>