JSP上传图片到MySQL数据库

本文详细介绍了一种在JSP项目中实现图片上传并存储到MySQL数据库的方法,包括数据库建表、使用eclipse进行开发、配置连接MySQL所需jar包等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**项目实战:如何制作有图片的评论

即:JSP上传图片到MySQL数据库**

**
预先准备:
MySQL
eclipse ee
mysql-connector-java-5.1.40-bin.jar包

MySQL数据库代码

**

CREATE DATABASE ImaUpdate;
#DROP DATABASE ImaUpdate;
CREATE  TABLE `ImaUpdate`.`images` (  
`id` INT NOT NULL ,  
`content` TEXT NULL ,  
`image` BLOB NULL ,  
PRIMARY KEY (`id`) );
ALTER TABLE `ImaUpdate`.`images` CONVERT TO CHARSET utf8;
INSERT INTO `ImaUpdate`.`images` (`id`, `content`) VALUES (0, '无');
#SHOW VARIABLES LIKE 'character%';

**

index.jsp

**

## 新的改变
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
 
<title>图片地址及评论内容传递</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>
 
<body>
	<form method="post" action="image.jsp">
		<input type="file" name="image" /><br /> 
		评论:<br />
		&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
		<textarea name="content" rows="7"  cols="25"></textarea><br />
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

image.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
 
<title>图片地址及评论内容传递</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>
 
<body>
	<form method="post" action="image.jsp">
		<input type="file" name="image" /><br /> 
		评论:<br />
		&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
		<textarea name="content" rows="7"  cols="25"></textarea><br />
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

Conn.java

package conDBMS;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
public class Conn {
	final String MYSQLDBDRIVER = "com.mysql.jdbc.Driver";
	final String MYSQLDBURL = "jdbc:mysql://localhost/ImaUpdate";
	final String MYSQLDBUSER = "root";
	final String MYSQLDBUSERPASS = "111";
 
	public Connection getConnection() {
		try {
			Class.forName(MYSQLDBDRIVER);
			Connection con = DriverManager.getConnection(MYSQLDBURL,
					MYSQLDBUSER, MYSQLDBUSERPASS);
			return con;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

此文借鉴于网络,原文地址:https://blog.youkuaiyun.com/LY_624/article/details/53453942
感谢原作者的无私奉献

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值