2013-04-01 217 views
0

我是一名Java初学者,并不理解如何获取我通过Java以Mysql数据库存储为Mediumblob的一段文本。以下是我遇到问题的部分代码。我已经通过.getString()和.getInt()以VARCHAR和INT的形式成功获得了其他存储的数据。我没有尝试下面的.getObject(),但似乎没有工作。任何见解将不胜感激〜谢谢。如何通过Java获取存储在Mysql Mediumblob中的文本?

try{ 
     con = DriverManager.getConnection(url, user, password); 
     pst = con.prepareStatement("SELECT * FROM exercise WHERE exercise_name = Clams;"); 
     rs = pst.executeQuery(); 

     while (rs.next()){ 

      java.sql.Blob id = rs.getBlob("description"); 
      System.out.print(id); 
     } 

    }catch(SQLException ex){ 

    }finally { 
     try { 
      if (rs != null){ 
       rs.close(); 
      } 
      if (pst != null){ 
       pst.close(); 
      } 
      if (con != null){ 
       con.close(); 
      } 
     }catch(SQLException ex){ 
     } 
     } 
    } 

回答

0

试试这个

java.sql.Blob id = rs.getBlob("description"); 
String s = new String(id.getBytes(0,id.length())); 
+0

感谢,我给它一个尝试,它编译,但没有出现在控制台上,但我刚刚意识到的东西...在数据库中我有这段文字保存,导入为.txt文件,所以我假设我需要使用fileReader来读取这个.txt文件,我试图从数据库中获取? – user2221016

相关问题