使用java向mysql保存图片路径

最近有一个项目有一个新需求,就是将好多图片的路径链接保存到mysql数据库中,一个一个手动保存是不存在的,于是用java写了个程序来实现
  • 测试图片:
    在这里插入图片描述
  • 数据库里建的表(随意建的):
    在这里插入图片描述
  • 代码:

    • DataBase.java:(需要填写自己的数据库地址、用户名、密码)
package com.beyes;
import java.sql.*;

public class DataBase {
   
    	private Statement stmt = null;
    	ResultSet rs = null;
    	private Connection conn = null;
    	String sql;
    	String strurl = "jdbc:(####自己的数据库地址####)?useSSL=false";
    
    	public DataBase() {
   
    	}
    
    	/**
    	 * 打开数据库连接
    	 */
    	public void OpenConn() throws Exception {
   
    		try {
   
    			Class.forName("com.mysql.jdbc.Driver");
    			conn = DriverManager.getConnection(strurl, "(####用户名####)", "(#####密码####)");
    			conn.setAutoCommit(false);
    		} catch (Exception e) {
   
    			System.err.println("OpenConn:" + e.getMessage())
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在Java中将图片路径保存到SQLyog数据库,您可以使用以下步骤: 1.创建一个新的数据库表,其中包含一个名为“image_path”的列,该列的数据类型为VARCHAR(或TEXT)。 2.使用INSERT语句将图像路径插入到新表中: String imagePath = "path/to/image.jpg"; String insertQuery = "INSERT INTO table_name (image_path) VALUES ('" + imagePath + "')"; //创建数据库连接 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); //创建语句对象并执行插入语句 Statement stmt = con.createStatement(); stmt.executeUpdate(insertQuery); //关闭数据库连接 stmt.close(); con.close(); 3.使用SELECT语句从表中检索图像路径: String selectQuery = "SELECT image_path FROM table_name"; //创建数据库连接 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); //创建语句对象并执行选择语句 Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(selectQuery); //循环遍历结果并输出 while(rs.next()) { String imagePath = rs.getString("image_path"); System.out.println(imagePath); } //关闭数据库连接 rs.close(); stmt.close(); con.close(); 4.您可以使用Java的图像处理库(如Java ImageIO)从图像路径中读取图像,并在应用程序中显示图像。例如: String imagePath = "path/to/image.jpg"; //使用Java ImageIO读取图像 File imageFile = new File(imagePath); BufferedImage image = ImageIO.read(imageFile); //将图像显示在Swing JFrame中 JFrame frame = new JFrame(); JLabel label = new JLabel(new ImageIcon(image)); frame.add(label); frame.pack(); frame.setVisible(true);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值