「java字段转换」字符转换成字符串 Java

博主:adminadmin 2023-01-15 00:06:09 456

本篇文章给大家谈谈java字段转换,以及字符转换成字符串 Java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java如何将所有要入库的数据全部转为大写?

如果只是希望输入的内容自动变大写,那么spring mvc 可以使用initbinder功能。可以自动将输入进行大写转换。 如果要在写数据前处理的话,需要aop来控制一下,快速的将bean中所有字段转换成大写,有个java工具叫 Dozer。功能很强。

后台IBATIS,那么可以typehandle来实现。

JAVA 中怎样把动态字段转换成静态字段?

在字段名前加上static关键字,可以将该字段定义为一个静态字段.

JAVA读取Oracle数据库Clob字段转换成String问题

如果使用oracle的话,直接将clob字段读取为string;也就是下面这一行:

clob

=

(oracle.sql.clob)

rs.getobject(1);

可以直接写成

string

str

=

rs.getstring(1);

java String类型和blob类型转换

如果你的数据真的是 String ,那按理就是用 Clob 嘛。

Blob 主要用于二进制内容,比如图片,附件。

如果保持数据库表结构不变的话,用 blob 也行,但你需要在读取和写入两头明确地指定相同的字符集,否则读取这个还原过程会得到不到期望的结果。只要我们用支持这种字符的字符集理论上来说,只要编码和解码的过程使用相同的字符集就不会失真,如果字符集本身不支持这个字符(比如你拿一个只有康熙字典中才有的古汉字用 GB2312 字符集去处理就会失真,而用 UTF8 就可能正常,因为只要这个字符真的能被输入法录入到电脑中基本上它就已经表示有办法能处理它)。

PreparedStatement 中有 setBlob (JDBC 4.0) 或 setBinaryStream (早期) 方法。但你需要测试你使用的驱动程序是什么版本的与数据库是否匹配。

java String类型转换为Blob类型怎么转

这个是mysql下存取blob字段的一个很简单的类,跟据自己的需要改改就行了

/**

* Title: BlobPros.java

* Project: test

* Description: 把图片存入mysql中的blob字段,并取出

* Call Module: mtools数据库中的tmp表

* File: C:\downloads\luozsh.jpg

* Copyright: Copyright (c) 2003-2003

* Company: uniware

* Create Date: 2002.12.5

* @Author: FeiFan

* @version 1.0 版本*

*

* Revision history

* Name Date Description

* ---- ---- -----------

* Chenqh 2003.12.5 对图片进行存取

*

* note: 要把数据库中的Blob字段设为longblob

*

*/

//package com.uniware;

import java.io.*;

import java.util.*;

import java.sql.*;

public class BlobPros

{

private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=windpassword=123useUnicode=true";

private Connection conn = null;

private PreparedStatement pstmt = null;

private ResultSet rs = null;

private File file = null;

public BlobPros()

{

}

/**

* 向数据库中插入一个新的BLOB对象(图片)

*

* @param infile - 要输入的数据文件

* @throws java.lang.Exception

*

*/

public void blobInsert(String infile) throws Exception

{

FileInputStream fis = null;

try

{

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

conn = DriverManager.getConnection(URL);

file = new File(infile);

fis = new FileInputStream(file);

//InputStream fis = new FileInputStream(infile);

pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");

pstmt.setString(1,file.getName()); //把传过来的第一个参数设为文件名

//pstmt.setBinaryStream(2,fis,(int)file.length()); //这种方法原理上会丢数据,因为file.length()返回的是long型

pstmt.setBinaryStream(2,fis,fis.available()); //第二个参数为文件的内容

pstmt.executeUpdate();

}

catch(Exception ex)

{

System.out.println("[blobInsert error : ]" + ex.toString());

}

finally

{

//关闭所打开的对像//

pstmt.close();

fis.close();

conn.close();

}

}

/**

* 从数据库中读出BLOB对象

*

* @param outfile - 输出的数据文件

* @param picID - 要取的图片在数据库中的ID

* @throws java.lang.Exception

*

*/

public void blobRead(String outfile,int picID) throws Exception

{

FileOutputStream fos = null;

InputStream is = null;

byte[] Buffer = new byte[4096];

try

{

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

conn = DriverManager.getConnection(URL);

pstmt = conn.prepareStatement("select pic from tmp where id=?");

pstmt.setInt(1,picID); //传入要取的图片的ID

rs = pstmt.executeQuery();

rs.next();

file = new File(outfile);

if(!file.exists())

{

file.createNewFile(); //如果文件不存在,则创建

}

fos = new FileOutputStream(file);

is = rs.getBinaryStream("pic");

int size = 0;

/* while(size != -1)

{

size = is.read(Buffer); //从数据库中一段一段的读出数据

//System.out.println(size);

if(size != -1) //-1表示读到了文件末

fos.write(Buffer,0,size);

} */

while((size = is.read(Buffer)) != -1)

{

//System.out.println(size);

fos.write(Buffer,0,size);

}

}

catch(Exception e)

{

System.out.println("[OutPutFile error : ]" + e.getMessage());

}

finally

{

//关闭用到的资源

fos.close();

rs.close();

pstmt.close();

conn.close();

}

}

public static void main(String[] args)

{

try

{

BlobPros blob = new BlobPros();

//blob.blobInsert("C:\\Downloads\\luozsh1.jpg");

blob.blobRead("c:/downloads/luozishang.jpg",47);

}

catch(Exception e)

{

System.out.println("[Main func error: ]" + e.getMessage());

}

}

}

java 字符串如何转换流存入blob字段中

将字符串转换成byte数组String.getBytes(),然后放进一个ByteArrayInputStream输入流中即可存入BLOB字段中

PreparedStatement.setBlob(int

parameterIndex,

InputStream

inputStream)

java字段转换的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于字符转换成字符串 Java、java字段转换的信息别忘了在本站进行查找喔。