谁有将mysql数据库编码转换的过滤器,我的JSP页面是GBK的,但mysql数据库是ISO8859_1的,现在想用过滤器转换 谁有将mysql数据库编码转换的过滤器,我的JSP页面是GBK的,但mysql数据库是ISO8859_1的,现在想用过滤器转换,谁有代码,及实现过程,感激不尽了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; public SetCharacterEncodingFilter() { super(); } /* * (非 Javadoc) * * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); } /* * (非 Javadoc) * * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Select and set (if needed) the character encoding to be used String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); // Pass control on to the next filter chain.doFilter(request, response); } /* * (非 Javadoc) * * @see javax.servlet.Filter#destroy() */ public void destroy() { this.encoding = null; this.filterConfig = null; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }}希望对你有所帮助 //数据库连接 private Connection getConnection() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); String dburl = "jdbc:mysql://" + "localhost" + "/" + "mydb" + "?user=" + "root" + "&password=" + "" + "&useUnicode=true&characterEncoding=GBK"; connection = DriverManager.getConnection(dburl); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } public static String toISO8859(String str) { if (str == null) { return null; } try { return new String(str.getBytes("iso-8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public static String toGBK(String str) { if (str == null) { return null; } try { return new String(str.getBytes("ISO8859_1"), "GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } String dburl = "jdbc:mysql://" + "localhost" + "/" + "mydb" + "?user=" + "root" + "&password=" + "" + "&useUnicode=true&characterEncoding=GBK";---------------------------------------------------------强烈不推荐,效率损失太大了!写一行不就完了嘛! 回LZ: 你把MYSQL的表转化成中文的不好吗? 用工具看数据还是中文的. SSH框架中的权限管理 ibatis事务 java.lang.ClassCastException js判断逻辑是否丢失 关于Struts2的注解 tomcat报错,请各位看看 json对返回object集合的处理方案 SOS:ntext类型里的中文字符如何处理以显示正常? 怎样配置Web服务器 ( 回答就有分 ) 新手求教,Entity报错 在action里调用javamail里的send方法不不能发送到邮件里,但是直接用main方法就可以!?????在线等 请问jfreechart图表中饼图可以显示数字吗?而不是百分比.
super(); } /*
* (非 Javadoc)
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding"); } /*
* (非 Javadoc)
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Select and set (if needed) the character encoding to be used
String encoding = selectEncoding(request); if (encoding != null)
request.setCharacterEncoding(encoding); // Pass control on to the next filter
chain.doFilter(request, response);
} /*
* (非 Javadoc)
*
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }}
希望对你有所帮助
private Connection getConnection() {
Connection connection = null; try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String dburl =
"jdbc:mysql://"
+ "localhost"
+ "/"
+ "mydb"
+ "?user="
+ "root"
+ "&password="
+ ""
+ "&useUnicode=true&characterEncoding=GBK";
connection = DriverManager.getConnection(dburl);
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
return connection;
} public static String toISO8859(String str) {
if (str == null) {
return null;
}
try {
return new String(str.getBytes("iso-8859-1"));
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
} public static String toGBK(String str) {
if (str == null) {
return null;
}
try {
return new String(str.getBytes("ISO8859_1"), "GBK");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
"jdbc:mysql://"
+ "localhost"
+ "/"
+ "mydb"
+ "?user="
+ "root"
+ "&password="
+ ""
+ "&useUnicode=true&characterEncoding=GBK";
---------------------------------------------------------
强烈不推荐,效率损失太大了!
写一行不就完了嘛!
回LZ: 你把MYSQL的表转化成中文的不好吗? 用工具看数据还是中文的.