**项目实战:如何制作有图片的评论
即: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 />
            
<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 />
            
<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
感谢原作者的无私奉献