怎么使用*.properties?还有中文的问题。(250分)

  • 主题发起人 chongqxiner
  • 开始时间
C

chongqxiner

Unregistered / Unconfirmed
GUEST, unregistred user!
本来想在大富翁里搜索一下,但是搜索功能好像有问题,只有在这里麻烦大家了。
1、Properties 先说说我的感觉,例如先在程序中定义一个:Properties m_propsDataSource = new properties();那么在程序中就可以通过get() ,put()来取存信息,我想问的是一般这种文件的使用时机是什么?如果有多个这样的文件程序怎么找到与之对应的那一个呢?等等一些与之相关的知识。
2、还有中文问题,一个web应用(目前我用的是javabean+servlet+jsp)可能经常会涉及到显示乱码的问题,,我暂时碰到了这几种情况1、从数据库中取值页面显示乱码(在数据库中值显示正常)2、将值存入数据库数据库(我只用过sqlserver)里显示为乱码,此外还有一些什么样的情况下会出现乱码的情况呢?我理解的乱码问题:因为在DB中的数据的存储是一种字符集(一般都是ISO 8859-1吧),而页面上我们需要显示的字符有是另外一种(比如:GB2312、GBK、UTF-8),还有java中采用UNICODE字符集,这样在交流时就产生了障碍,所以显示就罢了工。这样似乎只要我们在流入流出数据时进行一下转码,统一一下字符集,就不会出现问题了,但问题肯定不是这么简单吧,大家帮我总结一下吧,一般你们的解决方案是什么呢,有没有什么一劳永逸的办法呀。呵呵,我看问题都很浅显,希望各位不吝赐教,谢谢先。
 
1.先写jsp代码如下:
<%@ page contentType="text/html;
charset=GBK" %>
<%@ page import="java.util.*"%>
<html>
<head>
<title>
引用资源文件的演示
</title>
</head>
<body bgcolor="#ffffff">
<h1>
引用资源文件的演示例子
</h1>
<%
String sDriver="";
String sUser="";
String sPasswd="";
String sDsn="";
try
{
ResourceBundle res;
res = ResourceBundle.getBundle("testres");
sDriver = res.getString("DRIVER");
sUser = res.getString("USERID");
sPasswd = res.getString("PASSWORD");
sDsn= res.getString("DSN");
}
catch(Exception e){out.println("Error:ResFileNotFound!");return;}
out.println("DRIVER为:"+sDriver);
out.println("<br>USERID为"+sUser);
out.println("<br>PASSWORD为"+sPasswd);
out.println("<br>DSN为:"+sDsn);
%>
</body>
</html>
2.建立资源文件
主菜单->File->New File->Type选择properties,文件名testres,存放路径随便选择。
点击OK即可。
3.编辑资源文件.
写如以下内容:
DRIVER=COM.ibm.db2.jdbc.net.DB2Driver
DSN=jdbc:db2://192.168.11.3:38868/mser
USERID=db2inst1
PASSWORD=11c1mari
保存。
4.在工程里添加资源文件。
主菜单->Project->Add Files/Packages->选择刚才创建的testres.properties。
点击OK即可。
5.运行jsp,此时页面输出为:
引用资源文件的演示例子
DRIVER为:COM.ibm.db2.jdbc.net.DB2Driver
USERID为db2inst1
PASSWORD为11c1mari
DSN为:jdbc:db2://192.168.11.3:38868/mser
资源文件添加成功了!!!

2003-9-8 12:44:00
修改笔记 发表评语???
2003-9-8 19:12:42 关于存储.properties文件的操作参考连接:
http://www.javaresearch.org/article/showarticle.jsp?column=1&amp;thread=4289
package beanservlettest;
import java.util.*;
import java.io.*;
public class poptest {
public poptest() {
}
public static void main(String[] args) {
poptest poptest1 = new poptest();
try{ poptest1.saveProperties();}
catch(Exception e){
System.out.println("出错了:" + e.getMessage());
}
}
private void saveProperties() throws Exception {
Properties prop = new Properties();
FileOutputStream fos;
FileInputStream fis;
try {
fis = new FileInputStream(new File("c://config.properties"));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
throw new Exception("File config.properties NOT found!");
}
try {
prop.load(fis);
}
catch (IOException ex1) {
ex1.printStackTrace();
throw new Exception("Error while read from config.properties!");
}
System.out.println("other is:" + prop.getProperty("other"));
prop.setProperty("setting1", "value2");
System.out.println("other is:" + prop.getProperty("other"));
try {
fos = new FileOutputStream(new File("c://config.properties"));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
throw new Exception("File config.properties NOT found!");
} //save the properties
try {
prop.store(fos, null);
}
catch (IOException e) {
e.printStackTrace();
throw new Exception("Error when save the config.properties.");
}
}
}

2、还有中文问题
只要保证保存和取出的时候的编码一致就可以了。
举例如下:
你是以gb2312保存到数据库,那么你只要保证再以GB2312的编码方式取出,
就不存在乱码问题,
通常的乱码问题时因为,按GB2312的方式保存进去,取的时候却是按ISO8859-1的方式读出,这样就出现了编码问题。
 
先谢谢你的回复。
对了,你在第一个问题中使用的是什么工具jb么?
关于数据库中的字符集是不是要自己预先设置呢?如果要的话,像SQLserver和Orcale中怎么设置呢。
另外想大家帮我看看这段程序,它这是一种什么样的解决方式。
/**
* 通过JNDI资源取得到ORACLE数据库系统的DataSource.
* @return DataSource
* @throws Exception
*/
private static javax.sql.DataSource getJNDIDataSource2Oracle(String trJNDIName) throws Exception
{
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("No Context");
DataSource datasource = (DataSource) ctx.lookup(strJNDIName);
if (datasource instanceof BasicDataSource)
{
BasicDataSource bds = (BasicDataSource) datasource;
bds.addConnectionProperty("useUnicode", "true");
bds.addConnectionProperty("characterEncoding", "gb2312");
}
return datasource;
}
 
1.是JB
2.数据库有自己的字符集,你可以自己在数据库中设置的。
 
顶部