在Javax.naming 中提供了Context接口,
数据源的配置涉及到Server.xml和web.xml,需要在server.xml中加入如下内容:说明一下:我的数据库是MYsql
具体还有一些详细的选项例如:MaxActive等,参加Server.xml中说明
另外在Web.xml中加入如下内容:
配置以上内容后,只要在你的Jsp或Javabean 中按以下方式创建连接,就可以
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/testDB");
conn = ds.getConnection();
以上代码均测试成功,但是在Server.xml中配置数据库的URL中我不能加入useUnicode=true&characterEncoding=GBK,所以从数据库中取出来的汉字都是????
我用如下代码来解决这个问题:
public static String toChinese(String strvalue) {
try{
if(strvalue==null)
{
return null;
}
else {
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}catch(Exception e){
return null;
}
}
写Blog 的时候停了几次电,真是郁闷,数据库的连接池的概念我现在还是不太清晰,加强学习,看书去了1
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/374079/viewspace-131981/ ,如需转载,请注明出处,否则将追究法律责任。