网站开发积累(中文乱码+连接Access)

来源:互联网 发布:9.0电脑刺绣制版软件 编辑:程序博客网 时间:2024/05/18 21:47

(一)在编写网站的时候,会涉及到jsp,servlet,数据库之间传递中文字符,为防止出现乱码问题,总结如下:

1:针对Servlet

      request.setCharacterEncoding("GB2312"); //设置输入编码格式

      response.setContentType("text/html;charset=GB2312");  //设置输出编码格式

2:针对JSP代码

     为保证JSP向客户端输出时是采用中文编码方式输出的   

                 <%@page contentType="text/html;charset=gb2312"%>

     为让JSP能正确获得传入的参数,我们在JSP源文件头加  

                 <%request.setCharacterEncoding("GB2312");%>    

     为让JSP编译器能正确地解码我们的含有中文字符的JSP文件,我们需要在JSP源文件中指定我们的JSP源文件的编码格式   

                 <%@page pageEncoding="GB2312"%>

(二)Java连接Access,注意指定编码方式为GBK/GB2312

public static boolean add() throws SQLException, UnsupportedEncodingException
    {
        boolean result=false;
        try
        {
            //加载驱动
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // --Access 数据库路径  
            String dbpath = "f:\\Access.accdb";
            // --连接字符串characterEncoding=GBK;
            String url = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+ dbpath;
            //--指定数据编码方式
            Properties properties=new Properties();
            properties.put("charSet", "GBK");
            Connection conn = DriverManager.getConnection(url,properties);
               
            Statement statement = conn.createStatement();
            int count = statement.executeUpdate("insert into people (name,age) values('刘德华','52')");  
            if(count==1)
            {
                result=true;
            }
            statement.close();
            conn.close();
        } catch (ClassNotFoundException e)
        {
             e.printStackTrace();
        }
        return result;

    }


原创粉丝点击