mysql中的数据类型的长度限制

mysql中的所有数据类型:
mysql支持多种数据类型,大致可以分为四类: 数值型、浮点型、日期/时间和字符串(字符)类型
1、 数值型: Mysql支持所有标准sql数值数据类型。
包括 严格数值数据类型(INTEGER、SMALLINT、DECIMAL和NUMERIC),以及近似数值数据类型(FLOAT、REAL和DOUBLE PRECISION),关键字INT是INTEGER的同义词,关键字DEC是DECIMAL的同义词。作为SQL标准的扩展,MySQL也支持整数类型TINYINT、MEDIUMINT和BIGINT。下面的表显示了需要的每个整数类型的存储和范围:
这里写图片描述

2、 浮点型:
这里写图片描述

3、日期和时间类型
表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR。每个时间类型有一个有效值范围和一个”“值,当指定不合法的MySQL不能表示的值时使用”零”值。TIMESTAMP类型有专有的自动更新特性。
这里写图片描述
注: 在生产里,日期时间型,往往用的比较少,而是用数字类型来取代日期类型!

4、 字符串类型
字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET
这里写图片描述
 CHAR和VARCHAR类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。
  BINARY和VARBINARY类类似于CHAR和VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。
  有4种TEXT类型:TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT。这些对应4种BLOB类型,有相同的最大长度和存储需求。
ENUM是枚举类型
 SET是集合类型不同于ENUM类型,它是一个排列组合。假如有abc,它可以选择a或b或c,也有选择是ab,ac,bc,也可以选择abc。

这里写图片描述

2、 java插入blob数据

import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.sql.Blob;  
import java.sql.Connection;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import org.junit.Test;  
public class TestBlob {  
@Test  
    public void getBlob(){//读取Blob数据  
       Connection con = null;  
       PreparedStatement ps = null;  
       ResultSet rs = null;  
       try {  
             con = JDBCTools.getConnection();  
             String sql = "SELECT id,name,age,picture FROM animal WHERE id=5";  
             ps = con.prepareStatement(sql);  
             rs = ps.executeQuery();  
             if(rs.next()){  
                    int id = rs.getInt(1);  
                    String name = rs.getString(2);  
                    int age = rs.getInt(3);  

                    Object o = rs.getObject(4);
                    if ( o != null ) {
                        Blob picture = rs.getBlob(4);//得到Blob对象, 如果当前列对应的值存在null的情况,则此处会提示错误
                         //开始读入文件  
                        InputStream in = picture.getBinaryStream();  
                        OutputStream out = new FileOutputStream("cat.png");  
                        byte[] buffer = new byte[1024];  
                        int len = 0;  
                        while((len = in.read(buffer)) != -1){  
                               out.write(buffer, 0, len);  
                        }  
                    }
             }  
       } catch (Exception e) {  
        e.printStackTrace();  
       }  
}  
@Test  
    public void insertBlob(){//插入Blob  
       Connection con = null;  
       PreparedStatement ps = null;  
       try {  
             con = JDBCTools.getConnection();  
             String sql = "INSERT INTO animal(name,age,picture) VALUES(?,?,?)";  
             ps = con.prepareStatement(sql);  
             ps.setString(1, "TheCat");  
             ps.setInt(2, 8);  
             InputStream in = new FileInputStream("J:/test1/TomCat.png");//生成被插入文件的节点流  
             //设置Blob  
             ps.setBlob(3, in);  

             ps.executeUpdate();  
       } catch (Exception e) {  
        e.printStackTrace();  
       }finally{  
             JDBCTools.release(con, ps);  
       }  
}  
class JDBCTools {//JDBC工具类  用来建立连接和释放连接  
       public static Connection getConnection() throws Exception{//连接数据库  
             String driverClass = null;  
             String url = null;  
             String user = null;  
             String password = null;  

             Properties properties = new Properties();  

             InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties");  
             properties.load(in);  

             driverClass = properties.getProperty("driver");  
             url = properties.getProperty("jdbcurl");  
             user = properties.getProperty("user");  
             password = properties.getProperty("password");  
             Class.forName(driverClass);  
             return DriverManager.getConnection(url, user, password);  
       }  
       public static void release(Connection con , Statement state){//关闭数据库连接  
             if(state != null){  
                    try {  
                           state.close();  
                    } catch (SQLException e) {  
                           e.printStackTrace();  
                    }  
             }  
             if(con != null){  
                    try {  
                           con.close();  
                    } catch (SQLException e) {  
                           e.printStackTrace();  
                    }  
             }  

       }  
       public static void release(ResultSet rs , Connection con , Statement state){//关闭数据库连接  
             if(rs != null)  
             {  
                    try {  
                           rs.close();  
                    } catch (SQLException e) {  
                           e.printStackTrace();  
                    }  
             }  
             if(state != null){  
                    try {  
                           state.close();  
                    } catch (SQLException e) {  
                           e.printStackTrace();  
                    }  
             }  
             if(con != null){  
                    try {  
                           con.close();  
                    } catch (SQLException e) {  
                           e.printStackTrace();  
                    }  
             }  
       }  
}  
}  
public class Review {  
       public  Connection getConnection() throws Exception{//连接数据库  
             String driverClass = null;  
             String url = null;  
             String user = null;  
             String password = null;  

             Properties properties = new Properties();  

             InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties");  

             properties.load(in);  

             driverClass = properties.getProperty("driver");  
             url = properties.getProperty("jdbcurl");  
             user = properties.getProperty("user");  
             password = properties.getProperty("password");  
             Class.forName(driverClass);  
             return DriverManager.getConnection(url, user, password);  
       }  

}  

猜你喜欢

转载自blog.csdn.net/miracle_8/article/details/80289993
今日推荐