将java中的data保存到sql中

一、首先将MySQL中时间的类型设置为datetime
在这里插入图片描述
二、在java中设置时间
Date ne = new Date()
三、将添加的时间封装到扩展类,并通过调用service和到中的方法

itesmCustom.setCreatetime(new java.sql.Date(ne.getTime()));
itesmCustom.setDetail(“王牌”);
itemsQueryVo.setItesmCustom(itesmCustom);
itemsCustomService.addItems(itemsQueryVo);

service层和dao层以及mapper.xml文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
四、保存成功
在这里插入图片描述

总结: java.sql.Date sqlDate = newjava.sql.Date(ne.getTime()),,ne代表java中时间的变量名

Java,可以使用JDBC连接SQL Server数据库,并将图片以二进制数据的形式存储到数据。以下是一个简单的示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class SaveImageToSQLServer { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; FileInputStream fis = null; try { // 1. 加载SQL Server JDBC驱动程序 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2. 连接SQL Server数据库 String url = "jdbc:sqlserver://localhost:1433;databaseName=mydb"; String user = "sa"; String password = "mypassword"; conn = DriverManager.getConnection(url, user, password); // 3. 准备SQL语句 String sql = "INSERT INTO Images (name, data) VALUES (?,?)"; pstmt = conn.prepareStatement(sql); // 4. 读取图片文件 File imageFile = new File("C:\\image.jpg"); fis = new FileInputStream(imageFile); byte[] imageData = new byte[(int) imageFile.length()]; fis.read(imageData); // 5. 设置SQL语句参数 pstmt.setString(1, imageFile.getName()); pstmt.setBytes(2, imageData); // 6. 执行SQL语句 int rows = pstmt.executeUpdate(); System.out.println(rows + " rows affected"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 7. 关闭数据库连接和输入流 try { if (fis != null) { fis.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 以上代码将指定路径下的图片文件以二进制形式读取并插入到名为Images的数据库表。其,name列存储图片文件名,data列存储图片的二进制数据。可以根据实际需要修改表名、列名和SQL语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值