jsp mysql 插入 读取 图片
建立数据库:
我的数据库images
create table img (
id int primary key auto_increment,
name varchar(80),
pic longblob
)
要保证网站根目录 有个 images 文件夹
插入数据库 从本地文件夹
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<%
String path=application.getRealPath("/"); //取得网扎根目录
File fr=new File(path+"images"); //得到inages文件夹下面的所有的文件
String []names=fr.list();//得到所有的文件名称
//设置URL和驱动
String url="jdbc:mysql://localhost:3306/images";
String Drivers="com.mysql.jdbc.Driver";
//练级数据库 准备SLQ语句
Class.forName(Drivers);
String SQL="insert into img(name ,pic) values(?,?)";
Connection conn=DriverManager.getConnection(url,"root","root");
PreparedStatement pstmt=conn.prepareStatement(SQL);
try{
for(int k=0;k<names.length;k++)
//生成输入流对象
InputStream fin=new FileInputStream(path+"images//"+names[k].toString());
//赋值
pstmt.setString(1,names[k]);
pstmt.setBinaryStream(2,fin);
//执行
pstmt.execute();
}
}catch(SQLException e)
{
out.println(e.toString());
}
conn.close();
out.println("Success");
%>
</body>
</html>
读取图片 输出到 文件夹里面 images
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; cha
相关文档:
一、概述
JSP中有一块重要的技术:自定义标签(Custom Tag),最近这几天在学习Struts的时候发现Struts中使用了很多自定义标签,如html、bean等。所以我就做了个简单的试验,学习一下这种技术。
首先介绍一下这种技术吧!
1.优 ......
注意:之前我在网上找了好多的资料 NND都不能正常配置成功 搞得我是非常地郁闷!
找到这篇文章之后我终于配置成功了!哇好爽呀!只要按照 上面的步骤就一定能够成功的!
此文章的安装方法适用于Windows XP下的Apache+PHP+MySQL安装,同时也适用于Windows 2003系统下的安装和配置。
1. 安装环境
操作系统是 Window ......
MySQL MyIsam 存储引擎在创建索引的时候,索引键长度是有一个较为严格的长度限制的,所有索引键最大长度总和不能超过1000,而且不是实际数据长度的总和,而是索引键字段定义长度的总和。下面做个简单的测试,记录一下。
root@sky:~# mysql -u sky -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Command ......
表名为table的表内容如下
Year month value
2009 1 1.1
2009 2 1.2
2009 3 1.3
2009 4 1.4
2010 1 2.1
2010 2 2.2
2010 3 &nb ......
如果只是一个数据库的话,我们一般习惯这样写:mysql_select_db("guestbook");
mysql_query('select * from users');
但是如果是连接多个mysql,有可能出现同名的数据库,或者数据库中出现同名的表,所以就必须用完整的形式了,如下代码:
<?php
$link1 = mysql_connect('localhost1','root','root');
mysql ......