java(JSP)实现mysql5.1存取图片
哪位高手指点迷津,Java(JSP)实现MySQL图片的存取操作,有完整实例更好,万分感谢~~~...
哪位高手指点迷津,Java(JSP)实现MySQL图片的存取操作,有完整实例更好,万分感谢~~~
展开
展开全部
mysql数据库里可以存储,图片的相对路径,还有每个图片要有唯一的ID,在数据库里,可以设置成自动编号。楼主实在不懂,留一邮箱,我做个例子给你发过去。
具体代码
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p>
<table border="1">
<tr>
<td>图片Num</td>
<td>图片</td>
</tr>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("utf-8");
String uri = "jdbc:mysql://localhost:3306/test";//使用的是test数据库
String sql = "select picNum from pic "; //我建图片表 pic
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
out.print(e);
}
try{
conn = DriverManager.getConnection(uri,"root","soft");//我的用户名是 root ,密码 是 soft 根据你的需要 更改
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td><%=rs.getString("picNum")%></td>
<td><img src="tupian/<%=rs.getString("picNum")%>"></td>
</tr>
<%
}
}catch(Exception e){
out.print(e);
}
%>
</table>
</body>
</html>
附录:sql脚本
use test;
create table pic(
id int primary key auto_increment,
picNum varchar(10) unique
);
insert into pic(picNum) values('1.jpg');
insert into pic(picNum) values('2.jpg');
insert into pic(picNum) values('3.jpg');
insert into pic(picNum) values('4.jpg');
insert into pic(picNum) values('5.jpg');
insert into pic(picNum) values('6.jpg');
insert into pic(picNum) values('7.jpg');
具体代码
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p>
<table border="1">
<tr>
<td>图片Num</td>
<td>图片</td>
</tr>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("utf-8");
String uri = "jdbc:mysql://localhost:3306/test";//使用的是test数据库
String sql = "select picNum from pic "; //我建图片表 pic
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
out.print(e);
}
try{
conn = DriverManager.getConnection(uri,"root","soft");//我的用户名是 root ,密码 是 soft 根据你的需要 更改
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td><%=rs.getString("picNum")%></td>
<td><img src="tupian/<%=rs.getString("picNum")%>"></td>
</tr>
<%
}
}catch(Exception e){
out.print(e);
}
%>
</table>
</body>
</html>
附录:sql脚本
use test;
create table pic(
id int primary key auto_increment,
picNum varchar(10) unique
);
insert into pic(picNum) values('1.jpg');
insert into pic(picNum) values('2.jpg');
insert into pic(picNum) values('3.jpg');
insert into pic(picNum) values('4.jpg');
insert into pic(picNum) values('5.jpg');
insert into pic(picNum) values('6.jpg');
insert into pic(picNum) values('7.jpg');
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询