zoukankan      html  css  js  c++  java
  • Document

    因为这是通用编码,像中国通常使用的GBK、GB2312、Big5等只是针对中文而言,但是对其他文字就不适用了,为了使得这个问题的解决具有文字编码通用性,所以我这里设定了UTF8这个编码。

    编码一致性涉及到的四个方面为:应用程序编码、数据库系统编码、数据库编码、应用程序与数据库系统的连接编码。

    1.mysql的设置,我的系统字符设置是拉丁文,也是够够的,发现之后要记得修改啊

    2.java的设置,记得自己在刚开始的时候,设置的是:conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xsxx","root","123456");

    这样在存储的时候,就会出现中文存储的乱码

    其实,仅仅需要加上“?characterEncoding=utf8”就能够实现中文的存储。

    运行效果如下:

    3.代码:

    /*****
    * java连接mysql
    * @author yanlong
    *2017/5/7
    */
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    //import java.util.Collection;
    import java.sql.SQLException;

    //import javax.sql.Statement;

    public class TestJDBC {
    public static void main(String[] args){
    ResultSet rs=null;
    Statement stmt=null;
    Connection conn=null;
    try{
    /*加载并注册mysql的JDBC驱动*/
    Class.forName("com.mysql.jdbc.Driver");
    /*建立到mysql的连接*/

    //conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xsxx","root","123456");
    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xsxx?characterEncoding=utf8","root","123456");
    /*访问数据库,并执行sql语句*/

    stmt=conn.createStatement();
    /*添加记录*/
    System.out.println("添加记录后:");
    String sqll="insert into xs value (111111,'小公举','水利工程')";
    stmt.executeUpdate(sqll);
    rs=stmt.executeQuery("select * from xs");
    while(rs.next()){
    System.out.println(rs.getInt("id"));
    System.out.println(rs.getString("name"));
    System.out.println(rs.getString("major"));
    }
    rs=stmt.executeQuery("select *from xs");
    while(rs.next()){
    System.out.println(rs.getInt("id"));
    System.out.println(rs.getString("name"));
    System.out.println(rs.getString("major"));
    }

    }catch(ClassNotFoundException e){
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try{
    if(rs!=null){
    rs.close();
    rs=null;
    }
    if(stmt!=null){
    stmt.close();
    stmt=null;
    }
    if(conn!=null){
    conn.close();
    conn=null;
    }
    }catch(SQLException e){
    e.printStackTrace();

    }
    }
    }
    }

  • 相关阅读:
    iOS Simulator功能介绍关于Xamarin IOS开发
    Unity中制作游戏的快照游戏支持玩家拍快照
    手机数据抓包入门教程
    Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数
    Hierarchy视图里的Transform和Camera组件
    用JAVA编写MP3解码器——GUI(FFT)(转)
    功率W与dBm的对照表及关系(转)
    单鞭天线的长度计算方法(转)
    STM32F10X SPI操作flash MX25L64读写数据(转)
    利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)
  • 原文地址:https://www.cnblogs.com/chenyanlong/p/6830005.html
Copyright © 2011-2022 走看看