如何用JSP向SQL Server中加入图像数据?中文数据还是存不进SQL Server?(100分)

  • 主题发起人 主题发起人 dingyuan
  • 开始时间 开始时间
1.将你要保存的图像转换为byte[],然后用PreparedStatement对象的setBytes方法设定参数。
接着执行PreparedStatement.executeUpdate()
2.中文数据在存入进SQL Server之前转换一下,
byte[] temp=inStr.getBytes("iso-8859-1");
inStr=new String(temp);
 
1.第一个问题我会试一下,THANKS
2.第二个问题,我现在知道最好的办法是一个朋友告诉我的:
new String (MM_dbValues.toString().getBytes("8859_1"),"gb2312")
 
关于第一个问题,我这里有刚刚测试成功的例子,你可以参考!
//定义并读取文件
File file=new File("c:/temp/title.jpg");
InputStream in=new FileInputStream(file);
int filelength=(int)file.length();
//连接数据库
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.0.23/test", "root", "");
//将从文件中读出的二进制文件流存入数据库
PreparedStatement stm=con.prepareStatement("update user SET test=? where id='1'");
stm.setBinaryStream(1,in,filelength);
ResultSet result=stm.executeQuery();
第二个问题你可以如此解决:
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
}
return "null";
}
 
后台你用的是什么数据库?
关于i18n的东西都要烦死人了
 
斯人:数据库用什么格式存储图像,我是用SQL SERVER数据库,应该用img 格式
还是用varchar格式?取图像的时候如何取?
 
存储图像用image, 今天了也遇到这个问题了,不过我是存文本。 我看了一下MSSQL 的
T-SQL HELP,它说有三和格式,ntext,text,image. 你可以看一下:
Fixed and variable-length data types for storing large non-Unicode and Unicode
character and binary data. Unicode data uses the UNICODE UCS-2 character set.
ntext
Variable-length Unicode data with a maximum length of 230 - 1 (1,073,741,823)
characters. Storage size, in bytes, is two times the number of characters
entered. The SQL-92 synonym for ntext is national text.
text
Variable-length non-Unicode data in the code page of the server and with a
maximum length of 231-1 (2,147,483,647) characters. When the server code page
usesdo
uble-byte characters, the storage is still 2,147,483,647 bytes. Depending
on the character string, the storage size may be less than 2,147,483,647 bytes.
image
Variable-length binary data from 0 through 231-1 (2,147,483,647) bytes.
 
To: dingyuan
不知你是否解決了這個問題,如果解決了,能否告訴我你是使用何種JDBC 嗎?
To: eguy, 斯人,
我現在也要對數據庫做一大文本的處理,我用是text 類型(mssql),主要是存文件字符,
如在網上考貝一段文章存到資料庫。我已對字符做過編碼,bean也編譯通過。但是Tomcat 報如下
錯誤,
Exception Report:
javax.servlet.ServletException
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:481)
at i_00025tranet.manager._0002fintranet_0002fmanager_0002fpostdata_0002ejsppostdata_jsp_23._jspService
(_0002fintranet_0002fmanager_0002fpostdata_0002ejsppostdata_jsp_23.java:161)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
...
Root Cause:
java.lang.NoSuchMethodError
at i_00025tranet.manager._0002fintranet_0002fmanager_0002fpostdata_0002ejsppostdata_jsp_23._jspService
(_0002fintranet_0002fmanager_0002fpostdata_0002ejsppostdata_jsp_23.java:121)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
.....
是否是我的jdbc 沒有某些方法?
我的源代嗎如下:
<jsp:useBean id="workM" scope="page" class="dbpool.JDBCMSSQL" />
<% String Boardid = request.getParameter("boardid");
String Types = request.getParameter("types");
String Author = request.getParameter("author");
String nAuthor = new String(Author.getBytes("big5"),"iso-8859-1");
String Title = request.getParameter("title");
String nTitle = new String(Title.getBytes("big5"),"iso-8859-1");
String Face = request.getParameter("face");
String Texts = request.getParameter("texts");
String nTexts = new String(Texts.getBytes("big5"),"iso-8859-1");
String Linkbox = request.getParameter("linkbox");
if (Linkbox == null) Linkbox="N";
String Linkurl = request.getParameter("linkurl");
if (Linkurl == null) Linkurl="";
Date now = new Date();
String AddDate = DateFormat.getDateInstance().format(now);
String Clicks ="0";
String Returns ="0";
String IsNew = "Y";
String IsDel = "N";
String usql = "INSERT INTO dbo.T_articles(BoardId, Types, Author, Title, " +
" Faces, Clicks, Returns, AddDate, LinkBox , LinkURL, IsNew, IsDel)" +
"VALUES (" + Boardid + "," + Types + ",'" + nAuthor + "','" + nTitle +
"',"+ Face + "," + Clicks + "," + Returns + ",'" + AddDate + "','" +
Linkbox + "','" + Linkurl +"','" + IsNew + "','" + IsDel + "')";
long RS = workM.executeUpdate(usql);
if (RS > 0){
String Tsql = "select max(ids) as maxNo from T_articles";
ResultSet rs1= workM.executeQuery(Tsql);
rs1.next();
Tsql = "update T_articles set texts= ? where ids=" + String.valueOf(rs1.getInt("maxNo"));
out.print(Tsql);
int rev =workM.TextUpdate(Tsql, nTexts);
if (rev > 0) out.print("update data is ok");
workM.CloseTextUpdate();
%>
Bean 如下:
package dbpool ;
import java.sql.* ;
public class JDBCMSSQL{
public String url="jdbc:inetdae:10.28.1.254?Sql7=true";
public String login="sa";
public String password="";
public String ConDB="webdatas";
public ResultSet rs=null;
public ResultSetMetaData resultsMeta =null;
public Connection conn;
public Statement st;
public PreparedStatement stm;
public boolean status;
public String sql;
public int columns;
public long rowcount =0;
public int rcount = 0;

public JDBCMSSQL(){
try{
Class.forName("com.inet.tds.TdsDriver").newInstance();
}
catch(Exception e){
System.err.println("JDBCMSSQL():" + e.getMessage());
}
}
public int TextUpdate(String sql, String x){
try{
DriverManager.setLoginTimeout(10);
conn = DriverManager.getConnection(url,login,password);
conn.setCatalog(ConDB);
stm=conn.prepareStatement(sql);
stm.setString(1,x);
rcount = stm.executeUpdate();
}
catch(SQLException ex){
System.err.println("JDBCMSSQL.TextUpdate.executeUpdate: " + ex.getMessage());
}
return rcount;
}
是為什麼呢?
 
奇怪,今天什麼代碼也沒有改動,昨天(上面列出)的代碼,盡然正常了,沒有出錯,大文本也可
以存入數據據了。 ?????
 
关于中文问题:
只需用nvarchar,ntext等类型就行了
 
如何在网页上显示数据库(Orale&amp;Sqlserver)中BLOB中的图像文件???http://www.delphibbs.com/delphibbs/dispq.asp?lid=462626

200大富翁元,嫌少可再加大富翁元。。。?????????!!!!!!!!!!
 
中文问题我一向的建议是换一个商业服务器。如果你是使用的resin,可能要恭喜你了,你怎么可能解决中文问题?
 
多人接受答案了。
 
后退
顶部