Java JDBC operation of binary data, date and time

 

Binary data

mysql provides four types to store binary data:

  • TinyBlob can store up to 255 bytes
  • Blob can store up to 65KB
  • MediumBlob can store up to 16MB
  • LongBlob can store up to 4GB

 

 1 //从properties文件中加载数据库配置
 2         Properties properties = new Properties();
 3         InputStream inputStream = Class.forName("test.Test").getResourceAsStream("/mysql.properties");
 4         properties.load(inputStream);
 5 
 6         String driver = properties.getProperty("driver");
 7         String url = properties.getProperty("url");
 8         String user = properties.getProperty("user");
 9         String password = properties.getProperty("password");
10 
11         Class.forName(driver);
12         Connection connection = DriverManager.getConnection(url, user, password);
13         
14         //插入
15         String sql1="insert into test_tb (id,blob_col) values (?,?)";
16         PreparedStatement preparedStatement1 = connection.prepareStatement(sql1);
17         InputStream is = new FileInputStream("resource/a.png");
18         preparedStatement1.setInt(1,1);
19         preparedStatement1.setBlob(2,is);  //参数是InputStream型
20         preparedStatement1.executeUpdate();
21 
22         //查询
23         String sql2="select blob_col from test_tb where id=?";
24         PreparedStatement preparedStatement2=connection.prepareStatement(sql2);
25         preparedStatement2.setInt(1,1);
26         ResultSet resultSet = preparedStatement2.executeQuery();
27         if (resultSet.next()){
28             FileOutputStream os=new FileOutputStream("resource/b.png");
29             Blob blob = resultSet.getBlob("blob_col");
30             InputStream binaryStream = blob.getBinaryStream();
31             byte[] data=binaryStream.readAllBytes();
32             os.write(data);
33         }
34 
35         connection.close();

 

Data stored only file, the file names need to use a separate one to save.

Generally, we are to upload files to the server, stored in the database file path. If the file is very small, it can also save directly to the database.

 

 

 

date

mysql provided date represents a date format: yyyy-mm-dd.

1  // insert 
2          String SQL1 = "INSERT INTO test_tb (date_col) values (?)" ;
 3          PreparedStatement preparedStatement1 = Connection.prepareStatement (SQL1);
 4          a Date date1 = new new a Date (System.currentTimeMillis ());   // Get the current date. Using the current time stamp date constructed 
. 5          preparedStatement1.setDate (. 1 , date1);
 . 6          preparedStatement1.executeUpdate ();
 . 7  
. 8          // query 
. 9          String SQL2 = "SELECT from the date_col test_tb" ;
 10          the PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
11         ResultSet resultSet = preparedStatement2.executeQuery();
12         while (resultSet.next()){
13             Date date2 = resultSet.getDate("date_col");
14             System.out.println(date2);
15         }

 

 

 

time

Providing mysql time represents time in the format: HH: mm: ss, hh 24-hour clock.

And the operation method of the same date, the Date Time can be replaced, is the use of System.currentTimeMillis () to construct.

The difference is that the Date, Time can also use new java.util.Date (). GetTime () to construct. Date object java.util.Date () structure including both the current date, including the current time. getTime () Gets a long type of stamp.

 

 

 

 

Date Time

Timestamp mysql provided to store date time format: yyyy-mm-dd HH: mm: ss.

 1         //插入
 2         String sql1 = "insert into test_tb (timestamp_col) values (?)";
 3         PreparedStatement preparedStatement1 = connection.prepareStatement(sql1);
 4         Timestamp timestamp1 = new Timestamp(System.currentTimeMillis());  //也可使用new Date().getTime()来构造
 5         preparedStatement1.setTimestamp(1,timestamp1);
 6         preparedStatement1.executeUpdate();
 7 
 8 
 9         //查询
10         String sql2 = "select timestamp_col from test_tb";
11         PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
12         ResultSet resultSet = preparedStatement2.executeQuery();
13         while (resultSet.next()){
14             Timestamp timestamps2 = resultSet.getTimestamp("timestamp_col");
15 
16             Date date=new Date(timestamps2.getTime());   //此句的Date是java.util.Date
17             System.out.println(date);  //Sun Aug 18 18:56:35 CST 2019的形式,不友好
18 
19             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
 20 is              String formatDT = sdf.format (timestamps2); // with the specified format to format, the parameter may be a Timestamp, long time stamp type, a Date 
21 is              System.out.println (formatDT);   // 2019-08 -18 19:06:06 
22          }

2 may also be used to store, a storage Date, a storage Time.

 

 

Unspecified type Date, Time, Timestamp, is the package java.sql.

new Date (). getTime () is the Date java.util.Date, default date and time to take the current, java.sql.Date constructor needs parameters.

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11373517.html