jsp到mysql不能存入中文的解决

开发环境:eclipse3.3+myeclipse6.0+jdk6.0+tomcat6.0+mysql5.5
使用的技术:struts和hibernate

 

首先建议使用utf-8就不会有这样的问题;如果不使用utf-8,可以参考以下的方法:

1.在每个jsp文件的开头都加有
<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312"%>


2.mysql的一个配置文件my.ini 中,将default-character-set=latin1 改成default-character-set=gbk

 

3.mysql表的字符集都用的是gbk;

 

4.hibernate.cfg.xml为

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
<![CDATA[jdbc:mysql://localhost:3306/webdemodb?useUnicode=true&characterEncoding=GBK&mysqlEncoding=GBK ]]>
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">GBK</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
mysql_Demo
</property>


<mapping resource="com/mycompony/webdemo/po/Disposeinfo.hbm.xml" />
<mapping resource="com/mycompony/webdemo/po/Progress.hbm.xml" ></mapping>
<mapping resource="com/mycompony/webdemo/po/Role.hbm.xml" />
<mapping resource="com/mycompony/webdemo/po/User.hbm.xml" />
</session-factory>

</hibernate-configuration>

5.新建EncodingFilter.java文件:
package com.cs2c.webdemo.util;

import java.io.*;
import javax.servlet.*;

public class EncodingFilter implements Filter
{
protected String encoding = null;
protected FilterConfig config;

public void init(FilterConfig filterConfig) throws ServletException
{
this.config = filterConfig;
//从web.xml配置文件中获取编码配置
this.encoding = filterConfig.getInitParameter("Encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
if(request.getCharacterEncoding() == null)
{
String encode = getEncoding();
if(encode != null)
{
//设置request的编码方式
request.setCharacterEncoding(encode);
}
}
chain.doFilter(request,response);
}
public String getEncoding()
{
return encoding;
}
public void destroy()
{

}
}
===========================================================================
6.web.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.cs2c.webdemo.util.EncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!--页面请求编码过滤器结束-->
 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用JDBC连接数据库,然后使用SQL查询语句获取数据并存入变量中。以下是一个简单的示例: ```java <%@ page import="java.sql.*" %> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String url = "jdbc:mysql://localhost:3306/testdb"; // 数据库连接URL String user = "root"; // 数据库用户名 String password = "123456"; // 数据库密码 String sql = "SELECT * FROM user WHERE id = ?"; // SQL查询语句 try { Class.forName("com.mysql.jdbc.Driver"); // 加载JDBC驱动程序 conn = DriverManager.getConnection(url, user, password); // 建立数据库连接 pstmt = conn.prepareStatement(sql); // 创建预编译的SQL语句对象 pstmt.setInt(1, 1); // 设置SQL语句中的参数 rs = pstmt.executeQuery(); // 执行查询操作,返回结果集 if (rs.next()) { String name = rs.getString("name"); // 获取结果集中的数据并存入变量中 int age = rs.getInt("age"); System.out.println("Name: " + name + ", Age: " + age); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); // 关闭结果集 if (pstmt != null) pstmt.close(); // 关闭SQL语句对象 if (conn != null) conn.close(); // 关闭数据库连接 } catch (SQLException e) { e.printStackTrace(); } } %> ``` 以上示例中,我们首先创建了数据库连接URL、用户名和密码,并定义了要执行的SQL查询语句。然后,我们加载JDBC驱动程序并建立数据库连接。接着,我们创建预编译的SQL语句对象,并使用setInt()方法设置SQL语句中的参数。最后,我们执行查询操作并获取结果集中的数据,将其存入变量中。注意,以上示例仅供参考,具体实现方式需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值