欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

jsp圖片上傳mysql數據庫

林玟書2年前12瀏覽0評論

JSP圖片上傳MySQL數據庫

使用JSP進行圖片上傳至MySQL數據庫,需要以下步驟:

1. 創建上傳表

CREATE TABLE `image` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL DEFAULT '',
`content` longblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2. 編寫上傳頁面

<form action="addImage.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="imageFile" />
<input type="submit" value="上傳" />
</form>

3. 編寫addImage.jsp文件

<%@ page import="java.io.*,java.sql.*"%>
<%@ page import="javax.servlet.*,javax.servlet.http.*"%>
<%!
public Connection getConnection() throws SQLException,ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false","root","root");
}
%>
<%
Connection conn = null;
PreparedStatement ps = null;
String name = request.getParameter("name");
InputStream is = null;
try {
conn = getConnection();
String sql = "INSERT INTO image(name, content) VALUES (?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1,name);
is = request.getPart("imageFile").getInputStream();
ps.setBlob(2, is);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ps.close();
is.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
%>

通過以上步驟,即可實現JSP圖片上傳至MySQL數據庫。